2014-11-06 75 views
-1

我目前在从gridview到excel文件的导出结果中看到一个问题。我们使用.xls版本,并在打开时,我们看到一些用户等待1小时以打开一个约80000行,大约50MB的导出文件。在这1小时内,他们不能使用任何MS Office程序。注意这个文件也是格式化的。在C#中编写excel 2007二进制文件的问题

我已阅读解决此问题的一种方法是使用.xlsb格式。虽然我通过保存50MB文件来测试它,并看到显着的文件大小减少,但我在将文件写入.xlsb文件后遇到问题。

我得到的错误说"****Excel cannot open the file 'testme.xlsb' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.**"**

这是我使用的代码。你能帮忙吗?

try 
     { 

      HttpContext.Current.Response.Clear(); 
      HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".xlsb"); 
      HttpContext.Current.Response.ContentType = "application/vnd.ms-excel.sheet.binary.macroEnabled.12"; 
      StringWriter sw = new StringWriter(); 
      HtmlTextWriter tw = new HtmlTextWriter(sw); 
      ctrl.RenderControl(tw); 
      HttpContext.Current.Response.Write(sw.ToString()); 
      HttpContext.Current.Response.End(); 
     } 
     catch (Exception ex) 
     { 
      throw; 
     } 
    } 
+1

让我猜测:RenderControl呈现HTML ...尝试用记事本检查它。在那里我没有看到任何创建/放置Excel文件的代码作为响应流。阅读为:GridView的RenderControl()不会**导出任何东西,它会将控件本身渲染为HTML。 – 2014-11-06 16:28:59

+0

我想弄清楚你的xlsb文件的内容正在生成的位置....如果你将它存储到一个字节数组中,你可以摆脱你的字符串编写器和html编写器,一个二进制写入你的字节数组,然后关闭响应。 – user2366842 2014-11-06 16:35:26

+0

@AdrianoRepetti是的,我们正在渲染HTML。 ctrl是一个gridView,我们将格式化的内容导出到excel文件中 – user3821501 2014-11-06 19:09:26

回答

-1

将HTML转储到文件中并调用它Excel的工作时间很长,似乎已达到极限。

循环遍历实际的行,并使用库来编写一个合适的Excel文件,你会很开心。