我已經嘗試了許多解決方案,但沒有工作請幫我解決這個問題。 下面是即使釋放資源後SSIS腳本任務即使在釋放資源後也無法關閉Excel
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp = null;
Excel.Workbooks workbooks = null;
Excel.Workbook xlWorkbook = null;
Excel.Worksheet worksheet = null;
Excel.Range xlRange = null;
try
{
xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;
xlApp.AskToUpdateLinks = false;
workbooks = xlApp.Workbooks;
xlWorkbook = workbooks.Open("sample.csv", 2, true);
xlWorksheet = xlWorkbook.Sheets[1];
xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
for (int row = 2; row <= rowCount; row++)
{
//some logic
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally //releasing all resources
{
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
xlRange = null;
xlWorksheet = null;
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
xlWorkbook = null;
workbooks = null;
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
xlApp=null;
}
使用的代碼,仍然可以看到一個Excel進程