由 ASP.NET Web 应用程序导出的 Excel 文件上弹出警告
我注意到每次打开我的网络应用程序导出的 Excel 文件时总是会弹出此警告消息
文件名.xls,格式不同 比文件扩展名指定的要多。 验证该文件不是 已损坏......
这是使用的代码:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Single_Raw.xls"));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table tb = new Table();
TableRow tr = new TableRow();
tr.Cells.Add((rawRow((lblPOR1.Text.Substring(0, 4)), (lblPOR1.Text.Substring(5, 3)), (lblPOR1.Text.Substring(9, 3)), lblPNL.Text.ToString())));
TableCell cell3 = new TableCell();
cell3.Text = " ";
TableRow tr2 = new TableRow();
tr2.Cells.Add((rawRow((lblPOR2.Text.Substring(0, 4)), (lblPOR2.Text.Substring(5, 3)), (lblPOR2.Text.Substring(9, 3)), lblPNL.Text.ToString())));
tb.Rows.Add(tr);
tb.Rows.Add(tr2);
tb.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
I noticed that this warning message always pops up every time I open the excel file exported by my web app
filename.xls, is in a different format
than specified by the file extension.
Verify that the file is not
corrupted.....
Here's the code used:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Single_Raw.xls"));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table tb = new Table();
TableRow tr = new TableRow();
tr.Cells.Add((rawRow((lblPOR1.Text.Substring(0, 4)), (lblPOR1.Text.Substring(5, 3)), (lblPOR1.Text.Substring(9, 3)), lblPNL.Text.ToString())));
TableCell cell3 = new TableCell();
cell3.Text = " ";
TableRow tr2 = new TableRow();
tr2.Cells.Add((rawRow((lblPOR2.Text.Substring(0, 4)), (lblPOR2.Text.Substring(5, 3)), (lblPOR2.Text.Substring(9, 3)), lblPNL.Text.ToString())));
tb.Rows.Add(tr);
tb.Rows.Add(tr2);
tb.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信它需要不同的内容类型:
I believe that it needs a different content type:
我想这取决于您的环境认为 excel 文件应该具有什么 mime 类型;根据 mime 类型 的概述,有至少有这些常见的:
应该有一个工具或(配置)文件来检查配置并可能在您的环境中添加新的 mime 类型,所以至少你可以摆脱警告自己。
I guess it depends on what mime-type your environment thinks an excel-file should have; according to this overview of mime types, there are at least these common:
There should be a tool or (config) file to check for configured and possibly add new mime-types in your environment, so at least you can get rid of the warning yourself.
我遇到了同样的问题,通过将扩展名从文件名中删除来解决。因此“Single_Raw.xls”将是“Single_Raw”。
I had the same kind of problem, which was solved by leaving the extension out of the filename. So "Single_Raw.xls" would be "Single_Raw".