使用转发器将 table 导出到 excel
Export table with repeater to excel
我叫兰,拜托,我需要你的帮助。
//抱歉,我的语言是//
我需要将带有中继器的 Table html 导出到 excel。
我的table结构是:
<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
评论、代码或wave都很好期待。
局域网
PD:对不起我的语言
您需要将所有字符串输出包含在打开 table HTML 标记 <TABLE>
和关闭 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();
}
我叫兰,拜托,我需要你的帮助。 //抱歉,我的语言是//
我需要将带有中继器的 Table html 导出到 excel。
我的table结构是:
<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
评论、代码或wave都很好期待。 局域网 PD:对不起我的语言
您需要将所有字符串输出包含在打开 table HTML 标记 <TABLE>
和关闭 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();
}