从DataGrid到Excel的数据导出在WPF中不起作用

编程入门 行业动态 更新时间:2024-10-26 13:34:33
本文介绍了从DataGrid到Excel的数据导出在WPF中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在下面的代码片段中尝试将Datagrid数据导出到Excel。

I tried this below code snippet to export Datagrid data to Excel.

private void btnExportToExcel_Click(object sender, RoutedEventArgs e) { ExportDataToExcel ex = new ExportDataToExcel(); ex.ExportToExcel(); } public Microsoft.Office.Interop.Excel.Application ExportToExcel() { int i = 0; int j = 0; int k = 1; j=dgDisplay.Items.Count;//it returns j=0..what went wrong? var rows = GetDataGridRows(dgDisplay); Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook ExcelBook; Microsoft.Office.Interop.Excel._Worksheet ExcelSheet; ExcelBook = (Microsoft.Office.Interop.Excel._Workbook)ExcelApp.Workbooks.Add(1); ExcelSheet = (Microsoft.Office.Interop.Excel._Worksheet)ExcelBook.ActiveSheet; for (i = 1; i <= dgDisplay.Columns.Count; i++) { ExcelSheet.Cells[1, i] = dgDisplay.Columns[i - 1].Header.ToString(); } foreach (DataGridRow r in rows) { DataRowView rv = (DataRowView)r.Item; foreach (DataGridColumn column in dgDisplay.Columns) { if (column.GetCellContent(r) is TextBlock) { TextBlock cellContent = column.GetCellContent(r) as TextBlock; ExcelSheet.Cells[i + 1, k] = cellContent.Text.Trim(); k++; //MessageBox.Show(cellContent.Text); } } } ExcelApp.Visible = true; ExcelSheet = null; ExcelBook = null; ExcelApp = null; return ExcelApp; } public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid) { var itemsSource = grid.ItemsSource as IEnumerable; if (null == itemsSource) yield return null; foreach (var item in itemsSource) { var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow; if (null != row) yield return row; } }

它将DataGrid行返回为0.出了什么问题出了什么问题,可以找到它..解决它的想法?

It returns DataGrid row as 0.what went wrong what the issue ,could find it ..any idea to solve it?

推荐答案

public void ExportToExcel() { int i = 0; int k = 1,h = 1; string strFullFilePathNoExt = @C:\ Users \ TestExcel4.xls; GetDataGridRows(dgDisplay); var rows = GetDataGridRows(dgDisplay); Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application (); Microsoft.Office.Interop.Excel._Workbook ExcelBook; Microsoft.Office.Interop.Excel._Worksheet ExcelSheet; ExcelBook =(Microsoft.Office.Interop.Excel._Workbook)ExcelApp.Workbooks.Add(1); ExcelSheet =(Microsoft.Office.Interop.Excel._Worksheet)ExcelBook.ActiveSheet; for(i = 1; i< = dgDisplay.Columns.Count; i ++) { ExcelSheet.Cells [1,i] = dgDisplay。列[i - 1] .Header.ToString(); } foreach(DataGridRow r in r ows) { DataRowView rv =(DataRowView)r.Item; foreach(dgDisplay.Columns中的DataGridColumn列) { if(column.GetCellContent(r)is TextBlock) { TextBlock cellContent = column.GetCellContent(r) as TextBlock; ExcelSheet.Cells [h + 1,k] = cellContent.Text.Trim(); k ++; } } k = 1; h ++; } ExcelApp.Visible = false; ExcelBook.SaveAs(strFullFilePathNoExt,Excel.XlFileFormat.xlOpenXMLWorkbook,Missing.Value,Missing.Value,false,false,Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution。 xlUserResolution,true, Missing.Value,Missing.Value,Missing.Value); ExcelBook.Close(strFullFilePathNoExt,Missing.Value,Missing.Value); ExcelSheet = null; ExcelBook = null; ExcelApp = null; } 我找到了解决问题的方法..谢谢 public void ExportToExcel() { int i = 0; int k = 1, h = 1; string strFullFilePathNoExt = @"C:\Users\TestExcel4.xls"; GetDataGridRows(dgDisplay); var rows = GetDataGridRows(dgDisplay); Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook ExcelBook; Microsoft.Office.Interop.Excel._Worksheet ExcelSheet; ExcelBook = (Microsoft.Office.Interop.Excel._Workbook)ExcelApp.Workbooks.Add(1); ExcelSheet = (Microsoft.Office.Interop.Excel._Worksheet)ExcelBook.ActiveSheet; for (i = 1; i <= dgDisplay.Columns.Count; i++) { ExcelSheet.Cells[1, i] = dgDisplay.Columns[i - 1].Header.ToString(); } foreach (DataGridRow r in rows) { DataRowView rv = (DataRowView)r.Item; foreach (DataGridColumn column in dgDisplay.Columns) { if (column.GetCellContent(r) is TextBlock) { TextBlock cellContent = column.GetCellContent(r) as TextBlock; ExcelSheet.Cells[h+1, k] = cellContent.Text.Trim(); k++; } } k = 1; h++; } ExcelApp.Visible = false; ExcelBook.SaveAs(strFullFilePathNoExt, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value); ExcelBook.Close(strFullFilePathNoExt, Missing.Value, Missing.Value); ExcelSheet = null; ExcelBook = null; ExcelApp = null; } I found a solution to my problem..Thanks

更多推荐

从DataGrid到Excel的数据导出在WPF中不起作用

本文发布于:2023-11-10 06:06:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574554.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   数据   DataGrid   Excel   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!