本文介绍了从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中不起作用
发布评论