我有这样的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/