Click here to Skip to main content
14,771,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
           }
       }


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

1 solution

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
   
Comments
Member 12059989 20-Oct-15 12:53pm
   
What solution did you find ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900