【发布时间】:2017-07-08 16:10:51
【问题描述】:
我尝试了很多解决方案,但都没有奏效,请帮我解决这个问题。 以下是SSIS Script Task
中使用的代码 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进程
【问题讨论】:
标签: c# excel ssis etl excel-interop