相关疑难解决方法(0)

在C#中安全地处理Excel互操作对象?

我正在开发一个winforms c#visual studio 2008应用程序.该应用程序与excel文件对话,我正在使用Microsoft.Office.Interop.Excel;这个.

我想知道即使出现错误,如何确保对象被释放?

这是我的代码:

private void button1_Click(object sender, EventArgs e)
{
    string myBigFile="";
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
    if (result == DialogResult.OK) // Test result.
        myBigFile=openFileDialog1.FileName;

    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    Excel.Range range;

    string str;
    int rCnt = 0;
    int cCnt = 0;

    xlApp = new Excel.ApplicationClass();
    xlWorkBook = xlApp.Workbooks.Open(myBigFile, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, 1, 0);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); …
Run Code Online (Sandbox Code Playgroud)

c# excel interop dispose excel-interop

14
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

dispose ×1

excel ×1

excel-interop ×1

interop ×1