我在這裏使用EPPlus創建一些.XLSX文件時出現了一個奇怪的問題。我有一個包被創建,然後被輸出到響應。將.XLSX輸出爲響應時出現EPPlus錯誤
var file = new FileInfo(@"C:\Test.xlsx");
ExcelPackage package = new ExcelPackage(file);
//...code to output data...//
package.Save();
這個文件保存到我的本地C:正確地驅動,當我打開它,它的偉大工程
我有如下創建一個包。沒有錯誤或任何東西,格式是否正確等
不過,我現在想的輸出文件的響應流,所以我已經修改了代碼,我只好這個樣子:
ExcelPackage package = new ExcelPackage();
//...code to output data...//
MemoryStream result = new MemoryStream();
package.SaveAs(result);
context.Response.Clear();
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
context.Response.AddHeader("Content-Disposition", "attachment;filename=MissionDetails.xlsx");
result.WriteTo(context.Response.OutputStream);
context.Response.End();
但是當我運行此代碼,我在嘗試打開Excel文件時出現以下提示:
Excel found unreadable content in filename.xlsx. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes
單擊是顯示以下提示:
This file cannot be opened by using Microsoft Excel. Do you want to search the Microsoft Office Online Web site for a converter that can open the file?
我選擇No
這裏,然後打開Excel文件並顯示此錯誤:
Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
但文件然後加載罰款,似乎是正確的,一切都格式化。但是,每次我嘗試打開文件時,它都會提供相同的提示和錯誤消息。
Note: The code to output the data does not change for either of these scenarios.
有沒有人看過類似的東西?或者有什麼想法什麼可能導致此錯誤地保存文件只有當輸出到響應?
爲了測試的目的,您可以簡化文件的內容(即設置單個單元格的值),以確保文件內容與文件流相比沒有問題? – JustinStolle 2012-07-18 21:22:52
就像上面說的,內容和我將它保存到硬盤時相同。我已經測試了這一點並驗證了它不是我添加到單元格中的內容。響應必須是一些問題。 – j00b 2012-07-18 21:43:50
因此,創建一個非常簡單的文件流來驗證問題出在哪裏 - 如果是這樣的話,它可能與EPPlus沒有任何關係。 – JustinStolle 2012-07-18 21:47:56