ReleaseObject导出到Excel时出错

时间:2014-07-18 13:33:38

标签: c# wpf excel treeview

我在WPF中有一个treeView.Now根据我的要求我想将它导出到excel.I这里有一个代码在堆栈溢出 LINK

这是我在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 for Excel并为这些行提供错误..

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

请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:0)

您必须以正确的顺序释放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部分。