Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 alot 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'
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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();
     }
share|improve this answer
 
sorry, I think no need to change that, but here got error DataGrid dgGrid = new DataGrid(); –  user2357207 May 21 '13 at 8:13
 
it asked me to resolve using System.Windows.Forms.DataGrid or System.Web.UI.WebControls.DataGrid... –  user2357207 May 21 '13 at 8:15
 
Are you dynamically creating HTML Table on code behind file or using <asp:Table ID="Table1" runat="server"></asp:Table> in aspx page?? –  watraplion May 21 '13 at 8:52
 
I created the rows and cells in the design view then added innerhtml to each cell with text dynamically –  user2357207 May 21 '13 at 8:54
 
can show your aspx code? –  watraplion May 21 '13 at 9:01
show 10 more comments

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 :

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.