我的名字是Lan,拜托,我需要你的帮助。 //抱歉mi lenguage //
我需要将带有转发器的表格html导出为excel。
我的表结构是:
<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" />
代码:
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();
}
excel文件中的结果如下: 没有正确的格式 Image with output excel file
Coments,code或wave是很好的期待。 兰 PD:对不起我的语言
答案 0 :(得分:0)
您需要将所有字符串输出括在打开的表格标记<TABLE>
和关闭表格标记</TABLE>
之间。
例如,此代码应该有效......
string output = "<table>" + sw.ToString() + "</table>";
Response.Output.Write(output);
整个代码......
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();
}