2014-01-29 57 views
0

我想下載Excel文件在本地系統中,但在打開文件的位置,我得到錯誤:下載的Excel檔案不開放

The file format or extension does not match.

雖然在後端文件是相同的擴展,但我的正在錯誤 PFB代碼:

string ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
Response.ContentType = ContentType; 
Response.AppendHeader("Content-Disposition", "attachment; filename="+downloadedFileName); 


downloadedFileName= "Myfile.xlsx" 
+0

只是要確定:文件大小是否匹配?手動複製服務器時可以打開文件嗎?我沒有看到'Response.Write ...',你能粘貼代碼嗎? –

+0

我想你沒有提到文件擴展名。即XLSX或.xls – Shashank

+0

@PatrickHofman:該文件已被保存到本地系統。當我通過雙擊打開它,然後我得到錯誤不在代碼中。而後端的文件很好。 – Aquarius24

回答

0

它將保存爲details.xls

Response.ClearContent(); 
Response.Buffer = true; 
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Details.xls")); 
    Response.ContentType = "application/ms-excel"; 
0

你,因爲你不使用Response.TransmitFile()有此錯誤。您的代碼應該是:

string ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
Response.ContentType = ContentType; 
Response.AppendHeader("Content-Disposition", "attachment; filename="+downloadedFileName); 
Response.TransmitFile(FilePath); // full path here 
+0

感謝Taosique,但你可以修改代碼,這樣我可以詢問用戶的位置在哪裏保存文件。 – Aquarius24

+0

@ Aquarius24:我想你在問客戶端。這就是您的瀏覽器如何決定。例如,Chrome和IE會將其保存在Downloads文件夾中。這不能從服務器控制。 –

+0

對。 'Response.TransmitFile()'在服務器**上獲取文件**的路徑。 – Taosique