由 ASP.NET Web 应用程序导出的 Excel 文件上弹出警告

发布于 2024-11-08 11:28:37 字数 1390 浏览 2 评论 0原文

我注意到每次打开我的网络应用程序导出的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风疏影 2024-11-15 11:28:37

我相信它需要不同的内容类型:

HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

I believe that it needs a different content type:

HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
伴随着你 2024-11-15 11:28:37

我想这取决于您的环境认为 excel 文件应该具有什么 mime 类型;根据 mime 类型 的概述,有至少有这些常见的:

  • application/excel
  • application/vnd.ms-excel
  • application/x-excel
  • application/x-msexcel

应该有一个工具或(配置)文件来检查配置并可能在您的环境中添加新的 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:

  • application/excel
  • application/vnd.ms-excel
  • application/x-excel
  • application/x-msexcel

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.

巴黎夜雨 2024-11-15 11:28:37

我遇到了同样的问题,通过将扩展名从文件名中删除来解决。因此“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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文