我必须打开一个excel文件,并希望获取数据以对文档进行措辞。我使用此代码:
private Excel.Application excelapp;
Type ExcelType = Type.GetTypeFromProgID("Excel.Application");
dynamic ExcelInst = Activator.CreateInstance(ExcelType);
this.excelapp = ExcelInst;
this.workbook = this.excelapp.Workbooks.Open(Filename : this.filePath, ReadOnly: true);
我使用了所有技术来关闭/退出/处理已打开的进程(Process.Kill除外),但是这些无效。如何完全终止任何正在运行的后台任务?
完成后,您只需要调用Excel Quit类的Application
方法:
private Excel.Application excelapp;
Type ExcelType = Type.GetTypeFromProgID("Excel.Application");
dynamic ExcelInst = Activator.CreateInstance(ExcelType);
this.excelapp = ExcelInst;
this.workbook = this.excelapp.Workbooks.Open(Filename : this.filePath, ReadOnly: true);
' do whatever you need there
this.workbook.Save();
this.excelapp.Quit();
如果使用此方法时未保存的工作簿已打开,则Excel将显示一个对话框,询问您是否要保存更改。您可以通过在使用Quit
方法之前保存所有工作簿或将DisplayAlerts
属性设置为False
来防止此情况。当此属性为False
时,当您退出未保存的工作簿时,Excel不会显示对话框。它退出而不保存它们。
如果将工作簿的Saved
属性设置为True
而不将工作簿保存到磁盘,则Excel将退出,而不要求您保存工作簿。注意:这不会保存工作簿;它只是使其看起来像已保存。
我的理解是,由于互操作使用“ COM”对象,因此您需要“释放”那些对象。类似……
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(xlApp);
按该顺序。这应该从后台任务中释放那些对象
使用office-interop时,我在以下方法上取得了成功……设置所有Excel / Word / Outlook。等等。try/catch/finally
语句中的互操作内容。这几乎可以保证即使有异常,您的代码也将正确关闭并“释放”其使用的资源。否则,代码将始终易于泄漏资源。
Word.Application app = null;
Word.Document doc = null;
try {
app = new Word.Application();
//app.Visible = true;
doc = app.Documents.Add();
// do some stuff with the document…
doc.SaveAs2(filename);
MessageBox.Show("Document created successfully !");
}
catch (Exception e) {
MessageBox.Show("ERROR: " + e.Message);
}
finally {
if (doc != null) {
doc.Close();
Marshal.ReleaseComObject(doc);
}
if (app != null) {
app.Quit();
Marshal.ReleaseComObject(app);
}
}
我使用以下命令打开excel并阅读/更新。
Excel.Application app;
Excel.Workbook wb;
private void openAndRead()
{
app = new Excel.Application();
wb = app.Workbooks.Open(YourFileHere);
//do stuff here read or write
//app.ActiveSheet.Cells[1, "A"] = "write this in cell A1";
//string read= app.ActiveSheet.Cells[1, "A"];
}
private void closeExcel ()
{
app.DisplayAlerts = false; //this will stop popup questions from excel
wb.Close();
app.Quit();
}
如果您有信心EXCEL.EXE
流程会消失,则有一种方法可以实现它。可能这不是elegant解决方案,但至少它does有效。主要思想是抓住Excel窗口的句柄。有了此句柄,您可以获取其进程并将其关闭。
using System.Diagnostics;
using Excel = Microsoft.Office.Interop.Excel;
void Kill_Excel()
{
// Create instance of Excel application.
// If you don't make Excel window visible, this method WILL NOT work!
Excel.Application excel = new Excel.Application { Visible = true };
// Catch Excel window handle
IntPtr hwnd = new IntPtr(excel.Hwnd);
// Get EXCEL.EXE process
Process xlproc = Process.GetProcesses().Where(p => p.MainWindowHandle == hwnd).First();
// Do some operations
Excel.Workbook book = excel.Workbooks.Add();
Excel.Worksheet sheet = book.Sheets[1] as Excel.Worksheet;
sheet.Cells[1, 1] = "Hello!";
// Close Excel
book.Close(SaveChanges: false);
excel.Quit();
// Garbage collection
GC.Collect();
GC.WaitForFullGCComplete();
// Kill process - FOR SURE! :)
xlproc.Kill();
}