如何完全终止 EXCEL.exe 在 office.interop 中运行任务?



我必须打开一个 excel 文件,并希望获取数据以word文档。我使用这个代码:

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 除外(,但这些技术不起作用。如何完全终止任何正在运行的后台任务?

我的理解是,由于互操作使用"COM"对象,因此您需要"释放"这些对象。像...

Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(xlApp);

按此顺序。这应该从后台任务中释放这些对象

在使用办公室互操作时,我通过以下方法取得了成功... 设置所有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);
}
}

完成后,您只需调用 ExcelApplication类的 Quit 方法:

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 将退出而不要求您保存工作簿。备注:这不会保存工作簿;它只是让它看起来像是被保存了。

我使用以下方法打开 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,有一种方法可以实现它。也许这不是优雅的解决方案,但至少它确实有效。主要思想是捕获Excel窗口的句柄。有了这个句柄,你可以得到它的进程并关闭它。

警告:如果您不使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();
}

最新更新