我做了类似的事情.我创建一个Excel文件或打开一个现有的.我删除所有工作表并添加我自己的工作表.这是我用来确保关闭所有引用的代码:
workbook.Close(true, null, null); excelApp.Quit(); if (newSheet != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(newSheet); } if (rangeSelection != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(rangeSelection); } if (sheets != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets); } if (workbook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); } if (excelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); } newSheet = null; rangeSelection = null; sheets = null; workbook = null; excelApp = null; GC.Collect();
我已经用很多不同的选项对它进行了测试,并没有让它失败.
我做了类似的事情.我创建一个Excel文件或打开一个现有的.我删除所有工作表并添加我自己的工作表.这是我用来确保关闭所有引用的代码:
workbook.Close(true, null, null); excelApp.Quit(); if (newSheet != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(newSheet); } if (rangeSelection != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(rangeSelection); } if (sheets != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets); } if (workbook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); } if (excelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); } newSheet = null; rangeSelection = null; sheets = null; workbook = null; excelApp = null; GC.Collect();
我已经用很多不同的选项对它进行了测试,并没有让它失败.