本文整理汇总了C#中Microsoft.Office.Interop.Excel.Application.Quit方法的典型用法代码示例。如果您正苦于以下问题:C# Microsoft.Office.Interop.Excel.Application.Quit方法的具体用法?C# Microsoft.Office.Interop.Excel.Application.Quit怎么用?C# Microsoft.Office.Interop.Excel.Application.Quit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Office.Interop.Excel.Application
的用法示例。
在下文中一共展示了Microsoft.Office.Interop.Excel.Application.Quit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExportarDataGridViewExcel
private void ExportarDataGridViewExcel(DataGridView grd)
{
SaveFileDialog fichero = new SaveFileDialog();
fichero.Filter = "Excel (*.xls)|*.xls";
if (fichero.ShowDialog() == DialogResult.OK)
{
Microsoft.Office.Interop.Excel.Application aplicacion;
Microsoft.Office.Interop.Excel.Workbook libros_trabajo;
Microsoft.Office.Interop.Excel.Worksheet hoja_trabajo;
aplicacion = new Microsoft.Office.Interop.Excel.Application();
libros_trabajo = aplicacion.Workbooks.Add();
hoja_trabajo =
(Microsoft.Office.Interop.Excel.Worksheet)libros_trabajo.Worksheets.get_Item(1);
//Recorremos el DataGridView rellenando la hoja de trabajo
for (int i = 0; i < grd.Rows.Count ; i++)
{
for (int j = 0; j < grd.Columns.Count; j++)
{
hoja_trabajo.Cells[i + 1, j + 1] = grd.Rows[i].Cells[j].Value.ToString();
}
}
libros_trabajo.SaveAs(fichero.FileName,
Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal);
libros_trabajo.Close(true);
aplicacion.Quit();
}
}
示例2: ExportExcelFile
/// <summary>
/// 导出Excel表格文件
/// </summary>
public static void ExportExcelFile(DataTable ExcelTable)
{
try
{
string SaveExcelName = string.Empty;//保存的Excel文件名称
SaveFileDialog SFDialog = new SaveFileDialog();
SFDialog.DefaultExt = "xlsx";
SFDialog.Filter = "Excel文件(*.xlsx;*.xls)|*.xlsx;*.xls";
SFDialog.ShowDialog();
SaveExcelName = SFDialog.FileName;//获取保存的Excel文件名称
if (SaveExcelName.IndexOf(":") < 0) return ;
Microsoft.Office.Interop.Excel.Application XlsApp = new Microsoft.Office.Interop.Excel.Application();//创建Excel应用程序
object missing = System.Reflection.Missing.Value;
if (XlsApp == null)
{
MessageBoxEx.Show("无法创建Excel表格文件,您的电脑可能未安装Excel软件!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return ;
}
else
{
Microsoft.Office.Interop.Excel.Workbooks WkBks = XlsApp.Workbooks;//获取工作簿对像
Microsoft.Office.Interop.Excel.Workbook WkBk = WkBks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);//添加Excel表格模板
Microsoft.Office.Interop.Excel.Worksheet WkSheet = (Microsoft.Office.Interop.Excel.Worksheet)WkBk.Worksheets[1];//获取工作表格1;
Microsoft.Office.Interop.Excel.Range Ran;//声明Excel表格
int TotalCount = ExcelTable.Rows.Count;
int rowRead = 0;//读取行数
float PercentRead = 0;//导入百分比
//写入字段名
for (int i = 0; i < ExcelTable.Columns.Count;i++ )
{
WkSheet.Cells[1, i + 1] = ExcelTable.Columns[i].ColumnName.ToString();//获取表列名称
Ran=(Microsoft.Office.Interop.Excel.Range)WkSheet.Cells[1,i+1];//列名称写入单元格
Ran.Interior.ColorIndex = 15;
Ran.Font.Bold = true;//标题加粗
}
ProgressBarMsg ProgBarMsg = new ProgressBarMsg();
ProgressBarMsg.StepMaxValue = TotalCount;//获取进度条最大范围
ProgBarMsg.ShowDialog();//显示进度条
string ProgressPercent = string.Empty;//进度百分比
//写字段值
for (int j = 0; j < ExcelTable.Rows.Count; j++)
{
for (int k = 0; k < ExcelTable.Columns.Count; k++)
{
WkSheet.Cells[j + 2, k + 1] = ExcelTable.Rows[j][k].ToString();//写表格值
}
rowRead++;
PercentRead = ((float)rowRead * 100) / TotalCount;//导入进度百分比
ProgressPercent = PercentRead.ToString("0.00") + "%";
ProgressBarMsg.ProgressValue = ProgressPercent;//获取进度百分比
ProgressBarMsg.CurrentValue = rowRead;//获取当前进度值
Application.DoEvents();//处理当前在消息队列中所有windows消息
}
WkSheet.SaveAs(SaveExcelName, missing, missing, missing, missing, missing, missing, missing,missing);
Ran = WkSheet.get_Range(WkSheet.Cells[2, 1],WkSheet.Cells[ExcelTable.Rows.Count + 2,ExcelTable.Columns.Count]);//给工作表指定区域
//设置Excel表格边框样式
Ran.BorderAround2(missing,Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin,
Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,missing,missing);
Ran.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;//设置区域边框颜色
Ran.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;//连续边框
Ran.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;//边框浓度
if (ExcelTable.Columns.Count > 1)
{//设置垂直表格颜色索引
Ran.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].ColorIndex=Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
}
//关闭表格对像,并退出应用程序域
WkBk.Close(missing,missing,missing);
XlsApp.Quit();
ProgBarMsg.Close();//关闭进度条
}
}catch(Exception ex)
{
MessageBoxEx.Show(ex.Message, "异常提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
}
}
示例3: dataGridViewExportAsExcel
private void dataGridViewExportAsExcel(DataGridView dgv, string FileFullPath)
{
string strExamPath = FileFullPath.Substring(0, FileFullPath.LastIndexOf('\\'));
if (!System.IO.Directory.Exists(strExamPath))
{
MessageBox.Show(FileFullPath, "目录错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
excelApplication.EnableEvents = false;
excelApplication.Application.DisplayAlerts = false;
excelApplication.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet myWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelApplication.ActiveSheet;
excelApplication.Visible = false;
int nRowIndex = 0;
int nColumnIndex = 0;
object[,] strArr = new object[gvDataRecord.RowCount + 1, gvDataRecord.ColumnCount];
foreach (DataGridViewColumn dgvc in dgv.Columns)
{
strArr[nRowIndex, nColumnIndex] = dgvc.HeaderText;
++nColumnIndex;
}
++nRowIndex;
nColumnIndex = 0;
foreach (DataGridViewRow dgvr in dgv.Rows)
{
foreach (DataGridViewCell dgvcell in dgvr.Cells)
{
strArr[nRowIndex, nColumnIndex] = dgvcell.Value.ToString();
++nColumnIndex;
}
++nRowIndex;
nColumnIndex = 0;
}
string strExcelMaxColumnIndex = GetExcelMaxColumnIndex(dgv.ColumnCount, dgv.RowCount + 1);
Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)myWorkSheet.get_Range("A1",strExcelMaxColumnIndex);
myRange.get_Resize(dgv.RowCount + 1, dgv.ColumnCount);
try
{
myRange.Value2 = strArr;
myRange.Columns.AutoFit();
myRange.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
myWorkSheet.SaveAs(FileFullPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
MessageBox.Show("文件成功保存到了" + FileFullPath, "保存成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
excelApplication.Quit();
killexcel(excelApplication);
GC.Collect();
}
示例4: Print
public void Print(YellowstonePathology.Business.Search.ReportSearchList caseList, string description, DateTime printDate)
{
Microsoft.Office.Interop.Excel.Application xlApp;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = false;
Microsoft.Office.Interop.Excel.Workbook wb = xlApp.Workbooks.Add(@"\\CFileServer\documents\ReportTemplates\MolecularTesting\CaseList.xlt");
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];
ws.Cells[3, 1] = "Batch: " + description + " - " + printDate.ToShortDateString();
int rowPosition = 6;
for (int i = caseList.Count - 1; i > -1; i--)
{
ws.Cells[rowPosition, 1] = caseList[i].ReportNo;
ws.Cells[rowPosition, 2] = caseList[i].PanelSetName;
ws.Cells[rowPosition, 3] = caseList[i].PatientName;
ws.Cells[rowPosition, 4] = caseList[i].PhysicianName + " - " + caseList[i].ClientName;
ws.Cells[rowPosition, 5] = caseList[i].OrderedBy;
rowPosition++;
}
Object oMissing = Type.Missing;
Object oFalse = false;
ws.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.Close(oFalse, oMissing, oMissing);
xlApp.Quit();
}
示例5: GetExcelSheets
public List<string> GetExcelSheets(string excelFileName)
{
Microsoft.Office.Interop.Excel.Application excelFileObject = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBookObject = null;
workBookObject = excelFileObject.Workbooks.Open(excelFileName, 0, true, 5, "", "", false,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"",
true,
false,
0,
true,
false,
false);
Microsoft.Office.Interop.Excel.Sheets sheets = workBookObject.Worksheets;
// get the first and only worksheet from the collection of worksheets
List<string> sheetNames = new List<string>();
Regex regSheetName = new Regex("Sheet\\d+");
foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in sheets) {
if (!regSheetName.IsMatch(sheet.Name)) {
sheetNames.Add(sheet.Name);
}
Marshal.ReleaseComObject(sheet);
}
excelFileObject.Quit();
Marshal.ReleaseComObject(sheets);
Marshal.ReleaseComObject(workBookObject);
Marshal.ReleaseComObject(excelFileObject);
return sheetNames;
}
示例6: ConvertCsvToExcel_MicrosoftOfficeInteropExcel
public void ConvertCsvToExcel_MicrosoftOfficeInteropExcel()
{
StringBuilder content = new StringBuilder();
content.AppendLine("param1\tparam2\tstatus");
content.AppendLine("0.5\t10\tpassed");
content.AppendLine("10\t20\tfail");
using (TemporaryFile xlsxFile = new TemporaryFile(".xlsx"))
{
Clipboard.SetText(content.ToString());
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Microsoft.Office.Interop.Excel.Application();
// for excel visibility
//xlexcel.Visible = true;
// Creating a new workbook
xlWorkBook = xlexcel.Workbooks.Add(misValue);
// Putting Sheet 1 as the sheet you want to put the data within
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet) xlWorkBook.Worksheets.get_Item(1);
// creating the range
Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range) xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.Paste(CR, false);
xlWorkSheet.SaveAs(xlsxFile.FileName);
xlexcel.Quit();
Console.WriteLine("Created file {0}", xlsxFile.FileName);
}
}
示例7: OpenExcelDocs2
public static void OpenExcelDocs2(string filename, double[] content)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); //引用Excel对象
Microsoft.Office.Interop.Excel.Workbook book = excel.Workbooks.Open(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //引用Excel工作簿
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Sheets.get_Item(1); ; //引用Excel工作页面
excel.Visible = false;
sheet.Cells[24, 3] = content[1];
sheet.Cells[25, 3] = content[0];
book.Save();
book.Close(Type.Missing, Type.Missing, Type.Missing);
excel.Quit(); //应用程序推出,但是进程还在运行
IntPtr t = new IntPtr(excel.Hwnd); //杀死进程的好方法,很有效
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
//sheet = null;
//book = null;
//excel = null; //不能杀死进程
//System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); //可以释放对象,但是不能杀死进程
//System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
}
示例8: insertBTN
public void insertBTN(String query, String path)
{
sqlCon.ConnectionString = conn;
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter(query, sqlCon);
System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
da.Fill(dtMainSQLData);
DataColumnCollection dcCollection = dtMainSQLData.Columns;
// Export Data into EXCEL Sheet
Microsoft.Office.Interop.Excel.Application ExcelApp = new
Microsoft.Office.Interop.Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);
// ExcelApp.Cells.CopyFromRecordset(objRS);
for (int i = 1; i < dtMainSQLData.Rows.Count + 2; i++)
{
for (int j = 1; j < dtMainSQLData.Columns.Count + 1; j++)
{
if (i == 1)
{
ExcelApp.Cells[i, j] = dcCollection[j - 1].ToString();
}
else
ExcelApp.Cells[i, j] = dtMainSQLData.Rows[i - 2][j - 1].ToString();
}
}
ExcelApp.ActiveWorkbook.SaveCopyAs(path + "\\Results.xlsx");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
}
示例9: exportData
public string exportData(String query)
{
string conn = ConfigurationManager.ConnectionStrings["Conn"].ToString();
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = conn;
sqlConn.Open();
SqlDataAdapter da = new SqlDataAdapter(query, sqlConn);
System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
da.Fill(dtMainSQLData);
DataColumnCollection dcCollection = dtMainSQLData.Columns;
// Export Data into EXCEL Sheet
Microsoft.Office.Interop.Excel.Application ExcelApp = new
Microsoft.Office.Interop.Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);
// ExcelApp.Cells.CopyFromRecordset(objRS);
for (int i = 1; i < dtMainSQLData.Rows.Count + 2; i++)
{
for (int j = 1; j < dtMainSQLData.Columns.Count + 1; j++)
{
if (i == 1)
{
ExcelApp.Cells[i, j] = dcCollection[j - 1].ToString();
}
else
ExcelApp.Cells[i, j] = dtMainSQLData.Rows[i - 2][j - 1].ToString();
}
}
string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
if (Environment.OSVersion.Version.Major >= 6)
{
path = Directory.GetParent(path).ToString();
}
//This path belongs to the computer where the application is running.
//I have to find a way to get the path from the client side...
try
{
ExcelApp.ActiveWorkbook.SaveAs(path + "\\Desktop\\Results.xlsx");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
return "The file has been generated successfully and it is ready on your Desktop.";
}
catch (Exception e)
{
return "Some error has occured. Message from Server: " + e.Message;
}
}
示例10: Prc_Bcao_Chuan_Hoa
/*
* Modify by ManhTV3 on 27/04/2012
* Kết xuất file excel báo cáo tiến độ chuẩn hóa
* */
public static void Prc_Bcao_Chuan_Hoa(string p_sourcePath,
string p_destinPath)
{
Microsoft.Office.Interop.Excel.Application _excelApp;
_excelApp = new Microsoft.Office.Interop.Excel.Application();
// Mở file mẫu excel
Microsoft.Office.Interop.Excel.Workbook workBook =
_excelApp.Workbooks.Open(p_sourcePath, //Filename
Type.Missing, //UpdateLinks
Type.Missing, //ReadOnly
Type.Missing, //Format
Type.Missing, //Password
Type.Missing, //WriteResPassword
Type.Missing, //IgnoreReadOnlyRecommended
Type.Missing, //Origin
Type.Missing, //Delimiter
Type.Missing, //Editable
Type.Missing, //Notify
Type.Missing, //Converter
Type.Missing, //AddToMru
Type.Missing, //Local
Type.Missing); //CorruptLoad
// Lấy dữ liệu
using (CLS_DBASE.ORA _ora = new CLS_DBASE.ORA(GlobalVar.gl_connTKTQ))
{
try
{
_ora.TransStart();
// Kết xuất báo cáo tiến độ chuẩn hóa
string _sql = "SELECT * FROM vw_bc_ch;";
DataTable _dt = _ora.TransExecute_DataTable(_sql);
if (_dt.Rows.Count > 0)
CLS_EXCEL.Prc_Add_Sheets(workBook, "BaoCaoChuanHoa", _dt);
_dt.Clear();
workBook.SaveAs(p_destinPath,
Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
//_ora.TransCommit();
}
finally
{
// Đóng file mẫu excel
workBook.Close(false, p_sourcePath, null);
_excelApp.Quit();
CLS_EXCEL.Prc_releaseObject(workBook);
CLS_EXCEL.Prc_releaseObject(_excelApp);
}
}
}
示例11: ExpExcel
public static string ExpExcel(string path)
{
ReturnDoc obj_ReturnDoc = new ReturnDoc();
try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
worksheet.Cells[1, 1] = "管道名称";
// 数据查询
OleDbDataReader rst = null;
string ret = CommonQuery.qryRst("select * from PIPELINE", ref rst);
Int16 i = 2;
DicCache dic = DicCache.getInstance();
if (ret.Equals("0"))
{
while (rst.Read())
{
worksheet.Cells[i, 1] = rst["DATA1"].ToString();
i++;
}
rst.Close();
}
string sPath = path;
string filename = "海底管道数据导出.xls";
workbook.Saved = true;
workbook.SaveCopyAs(sPath + filename);
xlApp.Quit();
obj_ReturnDoc.addErrorResult(Common.RT_SUCCESS);
obj_ReturnDoc.setFuncErrorInfo(filename);
}
catch (Exception e)
{
obj_ReturnDoc.addErrorResult(Common.RT_FUNCERROR);
obj_ReturnDoc.setFuncErrorInfo(e.Message);
}
return obj_ReturnDoc.getXml();
}
示例12: ExportExcel
public void ExportExcel(string fileName, DataGridView myDGV)
{
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //被点了取消
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
//写入标题
for (int i = 0; i < myDGV.ColumnCount; i++)
{
worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
}
//写入数值
for (int r = 0; r < myDGV.Rows.Count; r++)
{
for (int i = 0; i < myDGV.ColumnCount; i++)
{
worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
}
catch (Exception ex)
{
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
xlApp.Quit();
GC.Collect();//强行销毁
MessageBox.Show("文件: " + fileName + ".xls 保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
示例13: toXLSX
public static void toXLSX(string filePath, string destination)
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// this does not throw exception if file doesnt exist
File.Delete(destination);
wb.SaveAs(destination, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, false, Type.Missing, Type.Missing, Type.Missing);
wb.Close(false, Type.Missing, Type.Missing);
app.Quit();
}
示例14: Export
public static FileInfo Export(ExcelPdfExportSettings exportSettings)
{
if (exportSettings == null) throw new ArgumentNullException("exportSettings");
if (exportSettings.OutputPdfPath.IsNullOrBlank()) throw new ArgumentException("OutputPdfPath is required.");
if (File.Exists(exportSettings.OutputPdfPath))
{
File.Delete(exportSettings.OutputPdfPath);
}
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application() { ScreenUpdating = false, DisplayAlerts = false }; ;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
try
{
// Create new instance of Excel and open Workbook
excelWorkbook = excelApplication.Workbooks.Open(exportSettings.WorkbookPath);
if (excelWorkbook == null)
{
throw new ApplicationException(String.Format("Specified Workbook '{0}' could not be found or may be corrupt.", exportSettings.WorkbookPath));
}
// Get Active sheet and Range to export to Pdf
var sheets = excelWorkbook.Sheets.OfType<Microsoft.Office.Interop.Excel.Worksheet>();
Microsoft.Office.Interop.Excel.Worksheet worksheet = sheets.FirstOrDefault(s => s.Name.Equals(exportSettings.WorksheetName, StringComparison.OrdinalIgnoreCase));
if (worksheet == null)
{
throw new ApplicationException(String.Format("Specified Workbook Sheet '{0}' not found.", exportSettings.WorksheetName));
}
Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range(exportSettings.WorksheetRange);
range.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, exportSettings.OutputPdfPath);
return new FileInfo(exportSettings.OutputPdfPath);
}
catch (Exception e)
{
throw e;
}
finally
{
if (excelWorkbook != null)
{ excelWorkbook.Close(); }
excelApplication.Quit();
excelApplication = null;
excelWorkbook = null;
}
}
示例15: GetContracts
/// <summary>
/// 根据
/// </summary>
/// <param name="contractFilePath"></param>
/// <returns></returns>
public static IEnumerable<Contract> GetContracts(string contractFilePath)
{
var app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = app.Workbooks.Open(contractFilePath, 0,
false, 5, System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
false, System.Reflection.Missing.Value,
System.Reflection.Missing.Value, true, false,
System.Reflection.Missing.Value, false, false, false);
//
//app.Visible = true;
var result = ContractCollector.GetContracts(app);
app.Quit();
return result;
}