本文介绍了导出到Excel时ReleaseObject错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在WPF中有一个treeView.Now根据我的要求,我想将其导出到excel. LINK
I have a treeView in WPF.Now as per my requirement i want to export it into excel.I have got a code here in stack overflowLINK
这是我在C#中的代码段.
Here is my code snippet in 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;
}
}
}
}
现在在上面的代码片段中,我不知道如何添加..
Now in the above code snippet i am not getting how to add ..
treeView_items.Items.Add(father);
我也无法为Excel发布releaseObject并给出这些行的错误.
Also i am not able to releaseObject for Excel also and giving error for these lines..
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
请帮助我.谢谢.
推荐答案
您必须以正确的顺序释放COM对象.我在 ExcelManager
类中有一个辅助方法:
You have to release the COM objects in the correct order. I have a helper method in my ExcelManager
class for this:
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
部分.
You could optionally remove the isQuitting
parts of the code.
这篇关于导出到Excel时ReleaseObject错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!