2012-12-11 43 views
2

當我嘗試打開下面的代碼創建的test.xlsx時,我得到「test.xlsx無法下載」。也就是說,如果我選擇保存文件,我可以保存並打開文件。test.xlsx無法下載 - EPPlus

請讓我知道我做錯了什麼。

謝謝!

Dim pack As New ExcelPackage 
    Dim ws As ExcelWorksheet = pack.Workbook.Worksheets.Add("Sheet1") 
    Dim ms As New MemoryStream 
    Dim dt As New DataTable 

    ws.Cells(1, 1).Value = "Test" 

    pack.SaveAs(ms) 

    ms.WriteTo(Context.Response.OutputStream) 

    Context.Response.Clear() 
    Context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" 
    Context.Response.AddHeader("Content-Disposition", "attachment;filename=test.xlsx") 
    Context.Response.StatusCode = 200 
    Context.Response.End() 
+0

什麼節目,你已經默認設置爲打開的.xlsx文件? – sacredfaith

+0

MS Excel和Internet Explorer – chaltahai

回答

0

我可能是錯的,但你寫你的文件Response.OutputStream然後調用Response.Clear,所以我想你respose頭被包含任何內容(沒有文件),這就是爲什麼它說:「無法發送被下載「。

嘗試移動ms.WriteTo到之前Response.End

+1

試過了,但沒有成功。 – chaltahai

0

下面的示例代碼應該做的工作:

Using pkg = New ExcelPackage() 
    Dim ws = pkg.Workbook.Worksheets.Add("Sheet 1") 
    ws.Cells(1, 1).Value = "Hello" 

    Dim buf = pkg.GetAsByteArray() 

    Response.Clear() 
    Response.AddHeader("Content-Disposition", "attachment;filename=test.xlsx") 
    Response.ContentType = MimeMapping.GetMimeMapping("*.xlsx") 
    Response.BinaryWrite(buf) 
    Response.Flush() 
    Response.[End]() 
End Using