1

如何发布为我的以下代码创建的 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 { } 

请检查这是否正确。

我知道这是重复的,我只要求你们检查这是否正确。

4

1 回答 1

0

Can use following:

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
于 2013-04-25T07:06:31.757 回答