我想将我的字符串html表导出为excel。尝试导出时,单击“保存”时出现以下异常
发送HTTP标头后,服务器无法添加标头
这是我的html表:
string tab = "<table cellpadding='5' style='border:1px solid black; border-collapse:collapse'>";
tab += "<tr><td style=' border-width:1px;border-style:solid;border-color:black;'>NumClient</td><td style=' border-width:1px;border-style:solid;border-color:black;'>Raison Sociale</td></tr>";
tab += "<tr><td style='border-width:1px;border-style:solid;border-color:black;'>" + NumClient + "</td><td style='border-width:1px;border-style:solid;border-color:black;'>"
+ Rs + "</td><td style='border-width:1px;border-style:solid;border-color:black;text-align:right;'> </td></tr>";
tab += "</table>";
这是控制器代码:
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=Reliquat.csv");
Response.ContentType = "text/csv";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write(tab);
Response.End();
当我点击继续时,我得到一个包含html代码的excel文件:
<table cellpadding='5' style='border:1px solid black; border-collapse:collapse'>";
tab += "<tr><td style=' border-width:1px;border-style:solid;border-color:black;'>NumClient</td><td style=' border-width:1px;border-style:solid;border-color:black;'>Raison Sociale</td></tr>
有人有解决方案吗?
答案 0 :(得分:1)
您可能想要使用以下代码。希望它有效。
Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = "application/excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Reliquat.xslx");
Response.Write(tab);
Response.Flush();
Response.Close();
Response.End();
从这里: http://www.codescratcher.com/asp-net/export-html-excel-asp-net/