导出到Excel时出现ReleaseObject错误

本文关键字:ReleaseObject 错误 Excel | 更新日期: 2023-09-27 18:13:35

我在WPF中有一个树视图。现在根据我的要求,我想把它导出到excel。我这里有一个堆栈溢出的代码链接

这是我的c#代码片段…

 private void button1_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog browse = new OpenFileDialog();
        browse.Filter = "Excel Worksheets|*.xls";
        if (browse.ShowDialog() == true)
        {
            Microsoft.Office.Interop.Excel.Application xlApp;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            Microsoft.Office.Interop.Excel.Range range;
            string str;
            int rCnt = 0;
            int cCnt = 0;
            xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(browse.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "'t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            range = xlWorkSheet.UsedRange;
            TreeViewItem father = new TreeViewItem();
            for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
            {
                for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
                {
                    str = (string)(range.Cells[rCnt, cCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
                    if (str != null)
                    {
                        father = new TreeViewItem();
                        father.Header = str;
                        GetChilds(father, cCnt + 1, rCnt, range);
                        treeView_items.Items.Add(father);
                    }
                } break;
            }

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

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }
    }
    private void GetChilds(TreeViewItem father, int cCnt, int rCnt, Microsoft.Office.Interop.Excel.Range range)
    {
        int col = cCnt;
        int row = rCnt;
        TreeViewItem child = new TreeViewItem();
        for (; col <= range.Columns.Count; col++)
        {
            for (; row <= range.Rows.Count; row++)
            {
                string str = (string)(range.Cells[row, col] as Microsoft.Office.Interop.Excel.Range).Value2;
                if (str != null)
                {
                    child = new TreeViewItem();
                    child.Header = str;
                    GetChilds(child, col + 1, row, range);
                    father.Items.Add(child);
                }
            } break;
        }
    }
}

}

现在在上面的代码片段中,我不知道如何添加…

treeView_items.Items.Add(father);

我也不能为Excel释放对象,并给出这些行错误..

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);

导出到Excel时出现ReleaseObject错误

必须按正确的顺序释放COM对象。我在我的ExcelManager类中有一个辅助方法:

    private void ReleaseComObjects(bool isQuitting)
    {
        try
        {
            if (isQuitting)
            {
                workbook.Close(false, missing, missing); 
                excelApplication.Quit();
            }
            Marshal.ReleaseComObject(workbooks);
            Marshal.ReleaseComObject(workbook);
            if (worksheets != null) Marshal.ReleaseComObject(worksheets);
            Marshal.ReleaseComObject(worksheet);
            Marshal.ReleaseComObject(excelApplication);
            workbook = null;
            worksheets = null;
            worksheet = null;
            excelApplication = null;
        }
        catch { }
        finally { GC.Collect(); }
    }

您可以选择删除代码的isQuitting部分