Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export from datatable to excel file in wpf c#

I have a datatable and want it to export it to excel file, it is a wpf application and all the solutions that i have found are for web application asp.net please help...

like image 445
Tony Avatar asked Jun 23 '12 08:06

Tony


1 Answers

just to make it better visible, for all

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;      

try
{
    excel = new Microsoft.Office.Interop.Excel.Application();
    wb = excel.Workbooks.Add();
    ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;

    for (int Idx = 0; Idx < dt.Columns.Count; Idx++)
    {
        ws.Range["A1"].Offset[0, Idx].Value = dt.Columns[Idx].ColumnName;
    }

    for (int Idx = 0; Idx < dt.Rows.Count; Idx++)
    {  // <small>hey! I did not invent this line of code, 
       // I found it somewhere on CodeProject.</small> 
       // <small>It works to add the whole row at once, pretty cool huh?</small>
       ws.Range["A2"].Offset[Idx].Resize[1, dt.Columns.Count].Value = 
       dt.Rows[Idx].ItemArray;
    }

    excel.Visible = true;
    wb.Activate();
}
catch (COMException ex)
{
    MessageBox.Show("Error accessing Excel: " + ex.ToString());
}
catch (Exception ex)
{
    MessageBox.Show("Error: " + ex.ToString());
}
like image 98
denza Avatar answered Sep 29 '22 16:09

denza