我正在使用C#生成几个电子表格,我正在尝试创建一个可重复使用的Style,可以应用于每个电子表格中的特定范围。我遇到的问题是当我的方法完成执行时Excel进程没有正确退出。事实上,我已经能够将问题区域缩小到我正在创建Excel Style
的代码中。
Workbooks books = excelApplication.Workbooks;
_Workbook wBook = books.Add("");
_Worksheet wSheet = (_Worksheet)wBook.ActiveSheet;
Styles styles = wBook.Styles;
Style columnHeader = styles.Add("ColumnHeader");
columnHeader.Font.Size = 12; // if I comment this out, excel quits correctly
Marshal.ReleaseComObject(wSheet);
Marshal.ReleaseComObject(wBook);
Marshal.ReleaseComObject(books);
当我应用它时,样式按预期工作,但退出excelApplication时Excel进程不会退出。如果我注释掉columnHeader.Font.Size = 12;
行,则Excel进程会正确退出。我错过了什么吗?
更新
我修改了Govert的示例WinForms应用程序,以反映我的类的结构,即创建多个电子表格。他的应用程序已正确退出Excel进程,但我的修改版本没有:
using System;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
namespace ExcelCOMReferenceTesting
{
public partial class Form1 : Form
{
private Excel.Application excelApplication;
private Excel.Style columnHeader;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DoMyExcelStuff();
GarbageCleanup();
}
private void DoMyExcelStuff()
{
StartExcel();
var wBook = GenerateWorksheet();
excelApplication.Range["A1"].Value = "Name";
excelApplication.Range["A1"].Style = columnHeader;
wBook.SaveAs(@"c:\Test\tst" + DateTime.Now.ToString("mmss") + ".xlsx");
// No need for Marshal.ReleaseComObject(...)
// No need for ... = null
StopExcel();
}
private void StartExcel()
{
excelApplication = new Excel.Application();
excelApplication.Visible = false;
}
private void StopExcel()
{
excelApplication.UserControl = false;
excelApplication.Quit();
}
private Excel._Workbook GenerateWorksheet()
{
Excel.Workbooks books = excelApplication.Workbooks;
Excel.Workbook wBook = books.Add("");
Excel.Styles styles = wBook.Styles;
columnHeader = styles.Add("ColumnHeader");
columnHeader.Font.Size = 12;
columnHeader.Font.Bold = true;
return wBook;
}
private void GarbageCleanup()
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
答案 0 :(得分:1)
可能是对Font / columnHeader或样式的挂起引用,访问Excel interop中属性的属性会导致Excel进程挂起;试试这个
Workbooks books = excelApplication.Workbooks;
_Workbook wBook = books.Add("");
_Worksheet wSheet = (_Worksheet)wBook.ActiveSheet;
Styles styles = wBook.Styles;
Style columnHeader = styles.Add("ColumnHeader");
Font font = columnHeader.Font;
font.Size = 12;
Marshal.ReleaseComObject(font);
Marshal.ReleaseComObject(columnHeader);
Marshal.ReleaseComObject(styles);
Marshal.ReleaseComObject(wSheet);
Marshal.ReleaseComObject(wBook);
Marshal.ReleaseComObject(books);
font = null;
columnHeader = null;
styles = null;
wSheet = null;
wBook = null;
books = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
答案 1 :(得分:1)
您永远不需要在此上下文中调用Marshal.ReleaseComObject
。运行时完全能够跟踪COM对象,并在不再引用它们时释放它们。调用Marshal.ReleaseComObject
是一个令人困惑的反模式,遗憾的是甚至有些微软文档错误地暗示了这一点。您也不必将本地变量设置为null
- 当方法完成时,引用将超出范围。
要关闭Excel,需要调用excelApplication.Quit()
,然后确保没有Excel COM对象的实时引用,然后调用垃圾收集器进行清理。您应该两次调用垃圾收集器 - 您可能会遇到引用形成循环的情况,并且第一次GC调用将打破循环,但COM对象可能只在第二次调用时正确释放。
在调试版本中,您还必须小心使用这种代码。方法中的引用被人为地保持活动直到方法结束,以便它们仍然可以在调试器中访问。这意味着可能无法通过在该方法中调用GC来清除本地Excel COM对象。要避免此问题,您可以遵循以下模式:
public void DoMyExcelStuffAndCleanup()
{
DoMyExcelStuff();
// Call GC twice to ensure that cleanup after cycles happens immediately
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
public void DoMyExcelStuff()
{
Application excelApplication = ...
// Here you access all those Excel objects ...
// No need for Marshal.ReleaseComObject(...)
// No need for ... = null
excelApplication.Quit();
}
我现在已经使用新的WinForms应用程序进行了测试,我在其中添加了一个按钮,这个代码背后了:
using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WinFormsComCleanup
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DoMyExcelStuff();
GarbageCleanup();
}
private void DoMyExcelStuff()
{
Excel.Application excelApplication = new Excel.Application();
Excel.Workbooks books = excelApplication.Workbooks;
Excel.Workbook wBook = books.Add("");
Excel.Worksheet wSheet = (Excel.Worksheet)wBook.ActiveSheet;
Excel.Styles styles = wBook.Styles;
Excel.Style columnHeader = styles.Add("ColumnHeader");
columnHeader.Font.Size = 12;
columnHeader.Font.Bold = true;
excelApplication.Range["A1"].Value = "Name";
excelApplication.Range["A1"].Style = columnHeader;
wBook.SaveAs(@"c:\Temp\tst" + DateTime.Now.ToString("mmss") +".xlsx");
// No need for Marshal.ReleaseComObject(...)
// No need for ... = null
excelApplication.Quit();
}
private void GarbageCleanup()
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
一切都按预期工作,Excel进程在GarbageCleanup()
调用期间停止。
<强>更新强>
更新问题后,我建议进行以下修改:
private void StopExcel()
{
excelApplication.UserControl = false;
excelApplication.Quit();
// Set the form-level variables to null, so that no live references to Excel remain
columnHeader = null;
excelApplication = null;
}
这将确保更新的问题中引入的对象级字段在GC运行之前将Excel的引用删除。
在我的测试中,通过此修改,Excel再次退出。这符合我的理解,即.NET运行时正确地跟踪COM引用,并且Excel互操作永远不需要Marshal.ReleaseComObject
。