我正在使用下面的代码从内存中释放 excel 表,但在代码运行后,excel 表仍在后台。请帮忙...
公共无效负载表(布尔导出,字符串项目名称){
xlApp = new Excel.ApplicationClass();
int rCnt = 0;
{
xlWorkBook = xlApp.Workbooks.Open(@"E:/try/" + DateTime.Now.ToString("M.d.yyyy")+@"/"+ projectName+ ".xlsx", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, true, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets["Upload Data"];
range = xlWorkSheet.UsedRange;
totalt.Text = range.Rows.Count.ToString();
currentID.Text = (range.Rows.Count+1).ToString();
Excel.Range).Value2;
xlWorkBook.Close(true, null, null);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
MessageBox.Show("Released"+obj.ToString());
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
在我从中获取所需数据后,我希望将 Excel 表从后台删除。