【问题标题】:C# Excel 2007 ErrorC# Excel 2007 错误
【发布时间】:2010-12-22 08:11:37
【问题描述】:

我有一个这样的简单代码。此代码只是将 gridwiev 导出到 excel 文件..

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=cat5.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");
//Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.Charset = "windows-1254";
//Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.ContentType = "application/vnd.ms-excel";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

但是我们公司有些用户使用Excel 2003,有些用户使用Excel 2007。当我运行这段代码时,Excel 2003没有问题。但是像这张图片一样,Excel 2007有一些问题。

我该如何解决这个问题?有人有想法吗?

最好的问候, 声纳。

【问题讨论】:

  • 所有网格尺寸都会发生这种情况吗?我从某个库导出到 Excel 时遇到了问题,该库通过附加一些空行来获得高于(我认为)7k 的文件大小,“解决”了。
  • 我不计算 gridsize 的任何文件大小还是什么?只需编写此代码并运行它。我的问题是 excel 2003 和 excel 2007 有什么不同..

标签: c# .net excel gridview


【解决方案1】:

我相信这是设计使然(在 Excel 2007 中):请参阅 http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx

可能的解决方法是在服务器上生成 csv 文件(但您会丢失格式)或生成实际的 excel 文件(使用 excel 自动化或第 3 方工具)。

【讨论】:

  • 我之前也遇到过这种情况,我指定了一个 .xlsx 扩展名,一切都很好。幸运的是,我可以相信我们都在使用 Office 2007,但这有点棘手。
【解决方案2】:

尝试删除此行:

Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");

然后Cell数据太大会被修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多