下面的代碼示例在Excel 2007工作得很好,但是當我安裝了Excel 2010(32位),它會離開Excel.exe進程打開的,除非我添加了GC.Collect的()。我的簡單問題是我做錯了什麼?它看起來像我釋放我使用的一切。Excel 2010中的COM對象的引用不釋放
public override void Update()
{
StatusBox.AddStatus("Opening File " + ImportPath);
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(ImportPath, 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);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];
Range rng = ws.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
int LastRow = rng.Row;
StatusBox.AddStatus(LastRow.ToString() + " Rows Found in File");
StatusBox.AddStatus("Closing File " + ImportPath);
System.Runtime.InteropServices.Marshal.ReleaseComObject(rng);
rng = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws = null;
wb.Close(true, ImportPath, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
GC.Collect();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
}
我用Excel和MapPoint做了一些COM互操作。我不認爲我曾經需要使用ReleaseComObject。清除所有空引用並調用相關的關閉/關閉方法已足夠。任何人都可以在這些Office類型的情況下確認/詳細說明ReleaseComObject()的可能需求嗎? – winwaed 2010-10-27 15:45:07
我在第一次編寫Excel 2003代碼時遇到掛起excel引用的問題時添加了ReleaseComObject。實際上,我已經閱讀了var = null;可能是不必要的。 – 2010-10-27 20:30:06