我需要将表中占位符的内容导出到excel中。当我只需将占位符放在div中时,就可以将数据(表)导出到excel。但是,当我将其放入表的td中时,我只能导出标题,而不能导出占位符的内容。
<asp:Panel ID="pnlMilestone" runat="server" CssClass="milestone-wrapper">
<div id="TableLists" runat="server" class="milestone-chart-wrapper">
<table>
<tr>
<td style="text-align: center;">
<asp:Label ID="lblChartTitle" runat="server" Visible="False" Font-Bold="True"></asp:Label>
</td>
</tr>
<tr>
<td>
<div runat="server" id="bartable_render">
<div id="bar_chart" style="height: 315px; width: 800px; margin-left: 150px;">
</div>
<div id="bar" style="height: 345px; width: 800px; margin-left: 10px;">
</div>
</div>
<asp:PlaceHolder ID="phDynamicTabular" runat="server"></asp:PlaceHolder>
</td>
</tr>
</table>
</div>
</asp:Panel>导出代码:
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=ProjectTarget_" + CampaignID + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
TableLists.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();发布于 2012-10-12 13:48:05
试试这段代码,
var htmlContent = GetHtmlContent();
var excelName="ProjectTarget_" + CampaignID;
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.ContentEncoding = Encoding.GetEncoding("windows-1254");
response.Charset = "windows-1254"; //ISO-8859-13 ISO-8859-9 windows-1254
response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}.xls", excelName));
response.ContentType = "application/ms-excel";
//response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
const string header = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title></title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1254\" />\n<style>\n</style>\n</head>\n<body>\n";
response.Write(header + htmlContent);
response.End();另请查看此页面:How to save created excel file to Client pc in ASP.NET
希望能有所帮助。
https://stackoverflow.com/questions/12852619
复制相似问题