DataTable export into Excel table in web-project

DataTable export into Excel table in web-project

I've been working on Asterisk billing fow a while (computing some kind of tables). And i've got a need to create quite complex reports periodically.

With my experience in coding, this task will be so difficult, so I decided to export my tables to Excel and then create report in this powerfull table processor.

Here is my function to export DataTable to Excel. Export can be triggered by pressing the button on web-form.

public void ExportToExcel(DataTable dt) {
  if (dt.Rows.Count > 0)
    string filename = "DownloadMobileNoExcel.xls"; 
    System.IO.StringWriter tw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
    DataGrid dgGrid = new DataGrid();
    dgGrid.DataSource = dt;

    //Get the HTML for the control.
    //Write the HTML back to the browser.
    //Response.ContentType = application/;
    Response.ContentType = "application/";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
    this.EnableViewState = false;

C-#, programming

  • Hits: 3088
Add comment

Related Articles