释放excel实例
本文关键字:实例 excel 释放 | 更新日期: 2023-09-27 18:11:00
如何释放为我下面的代码创建的excel实例?终止excel实例
Excel.Workbook Workbook1 = xCel.Workbooks.Open(ExcelFilePath);
Excel.Workbook Workbook2 = xCel.Workbooks.Add();
foreach (Excel.Worksheet dummysheet in Workbook1.Worksheets)
{
dummysheet.Copy(Workbook2.Worksheets[1],Type.Missing);
}
Workbook2.SaveAs(strFileCopyPath);
//代码释放excel实例
if (sourceWorkbook != null)
{
sourceWorkbook = null;
destinationWorkbook = null;
Thread.Sleep(500);
}
if (status.ProcessID > 0)
{
Process p = Process.GetProcessById(status.ProcessID);
p.Kill();
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
try { Marshal.FinalReleaseComObject(sourceWorkbook); }
catch { }
try { Marshal.FinalReleaseComObject(destinationWorkbook); }
catch { }
请检查是否正确。
我知道这是重复的,我只是想让你们检查一下这是否正确。
可以使用:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();