Export HTML Table to Excel using ASP.NET

前端 未结 5 1592
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 13:39

I have an html table (Not Gridview) and that has no proper Header and rows. Instead it has customized structure and data. I want to export this table to Excel. How can I do

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 14:13

    You want Export HTML table (Not Gridview) customized structure and data to Excel using ASP.NET.

    Try the following Approach

    1. Provide the ID and add runat="server" attribute

    2. Add the following code

      Response.ContentType = "application/x-msexcel"; 
      Response.AddHeader("Content-Disposition", "attachment;
      filename=ExcelFile.xls");
      Response.ContentEncoding = Encoding.UTF8; 
      StringWriter tw = new StringWriter();
      HtmlTextWriter hw = new HtmlTextWriter(tw);
      tbl.RenderControl(hw);
      Response.Write(tw.ToString());
      Response.End();
      
    3. 提交回复
      热议问题