Search code examples
c#.netexcelwpfdatagrid

How to export a DataGrid to Excel in WPF


I am working on a project and I have not been able to export the information that I have in my DataGrid to excel.

how can I do it?


Solution

  • Install Microsoft.Office.Interop.Excel Nuget package in your application. Right-click on your project -> "References" and choose "Manage NuGet Packages...", then just search for Excel. Otherwise, select Tools -> Nuget Package Manager -> Package Manager Console -> then install the Excel nuget (https://www.nuget.org/packages/Microsoft.Office.Interop.Excel/).

    Bind the items in DataGrid and then export data to excel as like below,

            private void btnExport_Click(object sender, RoutedEventArgs e)
            {            
                Microsoft.Office.Interop.Excel.Application excel = null;
                Microsoft.Office.Interop.Excel.Workbook wb = null;
                object missing = Type.Missing;
                Microsoft.Office.Interop.Excel.Worksheet ws = null;
                Microsoft.Office.Interop.Excel.Range rng = null;
    
                // collection of DataGrid Items
                var dtExcelDataTable = ExcelTimeReport(txtFrmDte.Text, txtToDte.Text, strCondition);
    
                excel = new Microsoft.Office.Interop.Excel.Application();
                wb = excel.Workbooks.Add();
                ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
                ws.Columns.AutoFit();
                ws.Columns.EntireColumn.ColumnWidth = 25;
    
                // Header row
                for (int Idx = 0; Idx < dtExcelDataTable.Columns.Count; Idx++)
                {
                    ws.Range["A1"].Offset[0, Idx].Value = dtExcelDataTable.Columns[Idx].ColumnName;                    
                }
    
                // Data Rows
                for (int Idx = 0; Idx < dtExcelDataTable.Rows.Count; Idx++)
                {  
                    ws.Range["A2"].Offset[Idx].Resize[1, dtExcelDataTable.Columns.Count].Value = dtExcelDataTable.Rows[Idx].ItemArray;
                }
    
                excel.Visible = true;
                wb.Activate();
            }