0

我正在使用下面的代码从内存中释放 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 表从后台删除。

4

1 回答 1

1

这样的双点调用表达式:

xlWorkBook = xlApp.Workbooks.Open(/*args*/);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets["Upload Data"];

你应该替换为

var workbooks = excel.Workbooks;
xlWorkBook  = workbooks.Open(/*params*/)
var worksheets = xlWorkBook.Worksheets;
xlWorkSheet = worksheets["Upload Data"];

//business logic here

Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(worksheets);    
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(excel);

有关更多信息,请参阅:https ://stackoverflow.com/a/17367570/976231

更新

xlApp = new Excel.ApplicationClass();
        int rCnt = 0;           
            {

            var workbooks = xlApp.Workbooks;
            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);
            var worksheets = xlWorkBook.Worksheets;
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets["Upload Data"];
            range = xlWorkSheet.UsedRange;
            var rows = range.Rows;
            totalt.Text = rows.Count.ToString();
            currentID.Text = (rows.Count + 1).ToString();               

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();


            Marshal.ReleaseComObject(rows);
            Marshal.ReleaseComObject(range);
            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(worksheets);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(workbooks);
            Marshal.ReleaseComObject(xlApp);
        }

    }
于 2013-07-02T06:56:15.553 回答