gpt4 book ai didi

c# - 如何使用asp.net C#将Html表格下载到excel文件中

转载 作者:行者123 更新时间:2023-11-30 20:48:09 25 4
gpt4 key购买 nike

我有一个这样的 Html 表:

<table cellpadding="0" cellspacing="0" border="0" class="Messages" id="idTbl" runat="server">
<tr>
<th>date</th>
<th>Subject</th>
</tr>
<tr class="myHot_smsMessages_new">
<td>01/01/2014</td>
<td>Some Subject No 1</td>
</tr>
<tr class="myHot_smsMessages_new">
<td>10/12/2013</td>
<td>Some subject no 2</td>
</tr>
</table>

在服务器端:

protected void ExportToXLS(object sender, EventArgs e)
{
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); idTbl.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}

有什么想法吗?

最佳答案

static StringBuilder StrBuilder = new StringBuilder();


protected void lnkExcel_Clicked(object sender, EventArgs e)
{
//ExportGrid.ExportExcel(griditem);
ConfigureEmployeeExport();

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/msexcel";
HttpContext.Current.Response.AddHeader("Content-Disposition", "filename=ExcelFile.xls");
HttpContext.Current.Response.Write(StrBuilder);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
}




public void ConfigureEmployeeExport()
{
StrBuilder.Append("<table cellpadding='0' cellspacing='0' border='0' class='Messages' id='idTbl' runat='server'><tr><th>date</th><th>Subject</th></tr><tr class='myHot_smsMessages_new'><td>01/01/2014</td><td>Some Subject No 1</td></tr><tr class='myHot_smsMessages_new'><td>10/12/2013</td><td>Some subject no 2</td></tr></table>");

}

关于c# - 如何使用asp.net C#将Html表格下载到excel文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25012579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com