iT邦幫忙

0

[ASP] 匯出Excel(.xls),打開會跳出警告訊息的解決方法? 如何匯出成Excel 2007以上(.xlsx)呢?

各位前輩好,小弟今天遇到一個找不到解決方法的問題,特上來請教各位前輩。

Response.AddHeader "Content-Disposition","attachment;filename=銷售明細表.xls"
	Response.ContentType="application/vnd.ms-excel"

使用如上程式碼加上HTML Tag,可以匯出Excel檔,只是會跳出如下錯誤訊息,但還是可以正常開啟Excel資料
https://ithelp.ithome.com.tw/upload/images/20191023/200172947LTiY9TAda.jpg

然而手邊有個匯出xlsx檔的範列,如下:

set xlApp = Server.CreateObject("Excel.Application")
....
....
location = Server.MapPath ("/autosendxls/xls/銷售明細表.xlsx")
	xlApp.ActiveWorkbook.Saveas location '儲存檔案

但是變成要指定存檔路徑,而不是另存新檔,請問我這樣的問題該如何解決呢?謝謝!

b7307024 iT邦新手 3 級 ‧ 2020-08-20 14:18:45 檢舉
最後還是使用Excel.Application這個方式匯出Excel,請問有誰可以來幫忙回答一下,以便結案,謝謝!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
player
iT邦大師 1 級 ‧ 2020-08-20 16:04:28
最佳解答

你幹嘛副檔名不一致? 是想在Server端轉檔嗎?

你這邊用(副檔名 .xls)
Response.AddHeader "Content-Disposition","attachment;filename=銷售明細表.xls"

可是開的卻是(副檔名 .xlsx)
location = Server.MapPath ("/autosendxls/xls/銷售明細表.xlsx")

至於SaveAs 的參數可以看官網
https://docs.microsoft.com/zh-tw/office/vba/api/excel.workbook.saveas
如果你真的要轉檔的話(.xlsx 轉 .xls ?),FileFormat那個參數記得傳你要的

看更多先前的回應...收起先前的回應...
b7307024 iT邦新手 3 級 ‧ 2020-08-20 16:32:11 檢舉

報告player大,不是要在Server端轉檔,兩段都是匯出Excel的語法喔!

此段語法是匯出舊版97~2003的格式

Response.AddHeader "Content-Disposition","attachment;filename=銷售明細表.xls"
Response.ContentType="application/vnd.ms-excel"

這個作法一直都有在使用,但是有缺點,有時候直接開啟會打不開且出現錯誤,要下載儲存檔案才可以打開,
https://ithelp.ithome.com.tw/upload/images/20200708/200172946Pmvw6xyiz.jpg

可以打開後又會先出現警告訊息,才可以打開
https://ithelp.ithome.com.tw/upload/images/20200708/20017294hHvuCH2SSD.jpg

才會改成

set xlApp = Server.CreateObject("Excel.Application")
....
....
location = Server.MapPath ("/autosendxls/xls/銷售明細表.xlsx")
	xlApp.ActiveWorkbook.Saveas location '儲存檔案

這樣可以直接匯出Excel 2007檔(.xlsx),打開不會有問題,而且檔案還比較小,缺點不是另存新檔,而且會在Server上留存檔案,後續才會衍生我請教您的Session_OnEnd刪除使用者暫存資料夾的問題。

player iT邦大師 1 級 ‧ 2020-08-20 16:47:55 檢舉

記得先輸出 ContentType
這是告訴瀏覽器要收哪種檔案

這是 .xls 用的
Response.ContentType="application/vnd.ms-excel"
Response.AddHeader "Content-Disposition","attachment;filename=銷售明細表.xls"

這是 .xlsx 用的
Response.ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader "Content-Disposition","attachment;filename=銷售明細表.xlsx"

player iT邦大師 1 級 ‧ 2020-08-20 16:50:32 檢舉

因為你是用ASP
而不是用ASP.NET
不然可以去接OpenXML SDK
開啟 .xlsx 檔後直接輸出
不必另外存暫存檔

b7307024 iT邦新手 3 級 ‧ 2020-08-20 17:05:35 檢舉

之前.xlsx用的語法有用過,今天再度測試一次,還是失敗,比.xls更慘,連內容都看不到,錯誤訊息如下:
https://ithelp.ithome.com.tw/upload/images/20200820/20017294c4WEBcfCAo.jpg

這個或許就是各位前輩建議我用.NET的原因吧,甚PHP也都可以採用直接輸出檔案的方式了,但是但是現階段的我還沒能力改阿~ 殘念!
/images/emoticon/emoticon02.gif

player iT邦大師 1 級 ‧ 2020-08-20 17:16:46 檢舉

你用 regedit 看看
HKEY_CLASSES_ROOT\.xlsx
裡面是否有 Content Type 的機碼?
因為我不知道你電腦裡的Excel是哪一版的?
我這邊的Excel 2019確定有application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 這個

b7307024 iT邦新手 3 級 ‧ 2020-08-20 18:04:46 檢舉

有的,我電腦是安裝Office 2010
https://ithelp.ithome.com.tw/upload/images/20200820/20017294T4numHxcvp.jpg

player iT邦大師 1 級 ‧ 2020-08-20 18:21:04 檢舉

那怪了? 怎麼會無法開啟 .xlsx檔呢?
你可以把那個檔案複製一份
把附檔名改成 .zip
再用能開zip檔的工具開看看嗎? (例如WinZIP, WinRar, 7-zip)
.xlsx 是基於 zip 基礎下的一堆xml檔案

b7307024 iT邦新手 3 級 ‧ 2020-08-20 18:34:37 檢舉

報告,也是不行,出現檔案已經毀損。
https://ithelp.ithome.com.tw/upload/images/20200820/20017294F7CzT0jMGa.jpg

player iT邦大師 1 級 ‧ 2020-08-20 18:43:57 檢舉

那你的檔案根本不是存成 xlsx 的吧?
看你要用Excel 打開重新存過嗎?
(另存新檔時要自己選一下檔案格式)

b7307024 iT邦新手 3 級 ‧ 2020-08-20 19:02:30 檢舉

有阿,另存的時候,預設就是Excel工作表了
https://ithelp.ithome.com.tw/upload/images/20200820/20017294BNmRrPrVdH.jpg

player iT邦大師 1 級 ‧ 2020-08-20 19:41:27 檢舉

https://www.office.com/launch/excel?ui=zh-TW&rs=TW&auth=1

你用MS官方的Web Excel開啟你的 xlsx 檔看看
有沒有錯誤訊息?
原本用 zip的工具去開,開不起來的話,就應該是存檔格式有問題了

b7307024 iT邦新手 3 級 ‧ 2020-08-21 01:38:54 檢舉

一樣無法打開,會有錯誤訊息
https://ithelp.ithome.com.tw/upload/images/20200821/20017294MRXrwHbPjf.jpg

b7307024 iT邦新手 3 級 ‧ 2020-08-21 15:15:04 檢舉

那目前來說,ASP+Excel匯出的解決方法,看起來使用excel.appliction是最佳方法,還是感謝palyer大跟我討論了這麼久,就感心捏! /images/emoticon/emoticon41.gif

我要發表回答

立即登入回答