如何发布为我的下面代码创建的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 { }
请检查这是否正确。
我知道这是重复的,所有我都要求你们检查这是否正确。
答案 0 :(得分:0)
可以使用以下内容:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();