c# - 导出到 excel - Windows Server 2008 IIS7 上的问题

标签 c# asp.net excel windows-server-2008 export

我正在尝试导出到 excel 中的 repeter/Gridview,这在我的本地计算机和 Windows Server 2003 中工作正常,但是当我在 Windows Server 2008 上部署时它无法正常工作。这是我的代码

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    string attachment = "attachment; filename=myReport.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/vnd.ms-excel";
    rpt.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.Flush();
    Response.End();

服务器详细信息:Windows Server 2008 和 IIS7

在 mozilla 中,所有页面内容都导出到 excel 时出错,但在 IE 和 Chrome 中,导出的是空 excel 文件,没有数据。

最佳答案

您正在向响应写入一个字符串。我希望这种文件类型是二进制的,因此您应该使用 Response.BinaryWrite()。 rpt.RenderControl(htw) 的实际内容是什么?我怀疑您应该将您的内容类型更改为 text/csv,Excel 仍会处理该类型。

关于c# - 导出到 excel - Windows Server 2008 IIS7 上的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3172274/

相关文章:

c# - sql server 数据库中的审计记录更改

asp.net - Orchard CMS 1.x [文档存储] - 混合记录支持和记录较少的属性

c# - 如何删除 Access Db 的临时 ldb 文件

Excel - 将以 Z 结尾的 ISO8601 日期转换为适合输入类型为 'timestamp with time zone' 的 Postgres 字段的格式

php - 使用来自另一列的字符串变量以及其他信息创建 MySQL 列

excel - 总结不同工作表中的真实情况

c# - 使用 HttpClient 和 Polly 发送并行请求,但每个主机只有一个,以优雅地处理 429 响应

c# - 未知错误 : insufficient parameters supplied to the command

c# - 一个对象中的多个列表

c# - 在 asp.net c# 中创建简单的 cookie