Search code examples
excelexporthtml-tablerepeater

Export table with repeater to excel


my name is Lan, Please, i need you help. //Sorry by mi lenguage//

i Need export a Table html with an repeater in to excel.

My table structure is:

 <table ID="TblExcel">
        <tr>
             <th style="">Id</th>
             <th style="">name</th>
             <th style="">date1</th>
             <th style="">Expire</th>
             <th style="">$</th>
       </tr> <asp:Repeater ID="datarepeat" runat="server">
             <ItemTemplate>
                  <tr>
                     <td ><%# DataBinder.Eval(Container.DataItem, "Id")%>/td>
                     <td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>
                     <td ><%# DataBinder.Eval(Container.DataItem, "date1", "{0:d/M/yyyy}") %></td>
                     <td ><%# DataBinder.Eval(Container.DataItem, "Date2", "{0:d/M/yyyy}") %></td>
>                     <td ><%# DataBinder.Eval(Container.DataItem, "Cost") %></td> /tr>
             </ItemTemplate>
         </asp:Repeater>
     </table>

  <asp:Button ID="Button1" runat="server" OnClick="ExportToExcel"
 Text="2Excel" />

Code:

protected void ExportToExcel(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Custodias.xls");
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.Charset = "UTF-8";
            Response.ContentType = "application/vnd.ms-excel.12";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            dlExcel.RenderControl(hw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

        }

And the result in excel file is this: NO correct format Image with output excel file

Coments, code or wave are good expectative. lan PD: sorry br my lenguage


Solution

  • You need to enclose all the string output between open table HTML tag <TABLE> and close table tag </TABLE>.

    For example, this code should work...

                string output = "<table>" + sw.ToString() + "</table>";
                Response.Output.Write(output);
    

    The entire code...

    protected void ExportToExcel(object sender, EventArgs e)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment;filename=Custodias.xls");
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.Charset = "UTF-8";
                Response.ContentType = "application/vnd.ms-excel.12";
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                dlExcel.RenderControl(hw);
                string output = "<table>" + sw.ToString() + "</table>";
                Response.Output.Write(output);
                Response.Flush();
                Response.End();
    
            }