CodeDon
CodeDon

Reputation: 77

ASP Table to Excel

When viewing ASPX in website, I got a table with few text in the cells, e.g.:

tab.Rows[1].Cells[1].innerHtml = "Booked :"

(In a lot of rows and cells but with different text in each cell)

Now I just want to click a button, and data in the table will be downloaded into an Excel file.

Table ID : tab

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Table tbl = new Table();
        TableRow tr = new TableRow();
        TableCell tcel = new TableCell();
        tcel.Text = "id";
        tr.Cells.Add(tcel);

        TableCell tcel1 = new TableCell();
        tcel1.Text = "id1";
        tr.Cells.Add(tcel1);

        tab.Rows.Add(tr);
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    string filename = "ExportExcel.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 = tbl;
    //dgGrid.DataBind();

    //Get the HTML for the control.             
    tab.RenderControl(hw);
    //Write the HTML back to the browser.
    //Response.ContentType = application/vnd.ms-excel;
    Response.ContentType = "application/vnd.ms-excel";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
    this.EnableViewState = false;
    Response.Write(tw.ToString());
    Response.End();
}

Modified with watraplion answer, but still not answer.. Error at:

DataTable dt = dt; //Use of unassigned local variable 'dt'

Upvotes: 0

Views: 7691

Answers (2)

Antonio Bakula
Antonio Bakula

Reputation: 20693

First to say that this what you are trying to do is not export to excel, but you send a html with a wrong headers to trick browser to open this content with excel.

This solution has many problems, excel it self will warn user that content is different from extension, because you send html and your response headers are saying that this is a excel file. And I can bet that some antimalware software on client or something similar on server, will block this response since serving different content than declared in headers is known malware behavior.

It's far more better and easier to use dedicated excel library, for example EPPlus, look here for ASP.NET ashx handler that exports DataTable to real excel file :

https://stackoverflow.com/a/9569827/351383

Upvotes: 0

watraplion
watraplion

Reputation: 287

Try this :

aspx Design View :

<body>
   <form id="form1" runat="server">
      <div>
          <asp:Table ID="Table1" runat="server">
           </asp:Table>
      </div>
      <div>
           <asp:Button ID="btnExport" onclick="btnExport_Click" Text="Export" runat="server">
      </div>
   </form>
</body>

aspx.cs ( code behind )

    protected void Page_Load(object sender, EventArgs e)
    {
        TableRow tr = new TableRow();
        TableCell tcel = new TableCell();
        tcel.Text = "id";
        tr.Cells.Add(tcel);

        TableCell tcel1 = new TableCell();
        tcel1.Text = "id1";
        tr.Cells.Add(tcel1);

        Table1.Rows.Add(tr);
    }

    protected void btn_Click(object sender, EventArgs e)
    {
        string filename = "ExportExcel.xls";
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

        //Get the HTML for the control.             
        Table1.RenderControl(hw);
        //Write the HTML back to the browser.
        //Response.ContentType = application/vnd.ms-excel;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
        this.EnableViewState = false;
        Response.Write(tw.ToString());
        Response.End();
     }

Upvotes: 1

Related Questions