2014-11-06 71 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文件,你會很開心。