最近剛好碰上在 Java Servlet 上, 
把網頁的資料轉成 Excel 檔的程式 , 在此紀錄一下...


這是一般網頁的格式
--輸出
HTTP/1.1 200 OK
Date: Wed, 14 Oct 2009 02:48:37 GMT
Server: Oracle-Application-Server-10g/9.0.4.3.0 Oracle-HTTP-Server
Pragma: no-cache
Cache-Control: no-cache
Keep-Alive: timeout=5, max=500
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=euc-kr


這是 Excel 檔案格式,以供下載
--原始碼
response.setContentType("text/html; charset=euc-kr");
--輸出
HTTP/1.1 200 OK
Date: Wed, 14 Oct 2009 02:48:52 GMT
Server: Oracle-Application-Server-10g/9.0.4.3.0 Oracle-HTTP-Server
Pragma: no-cache
Cache-Control: no-cache
Content-Disposition: inline;filename=BOM_TXTHSM1908000BT.xls
Keep-Alive: timeout=5, max=500
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/x-excel;name='My_excel';charset=euc-kr
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
--原始碼
response.setContentType("application/x-excel;name='My_excel';charset=euc-kr");
response.setHeader("Content-Disposition","inline;filename="+date_wc+".xls");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=euc-kr\">");


另外發現這個網頁有 ContentType 可供參考
http://blog.sina.com.tw/supermomo/article.php?pbgid=51237&entryid=574444


一些較常用的 ContentType
GIF images ==> image/gif"   
JPEG images ==> image/jpeg"  
TIFF images ==> image/tiff" 
MICROSOFT WORD document ==> application/msword"   
RTF document ==> application/rtf"   
MICROSOFT EXCEL document ==> application/x-excel"   
MICROSOFT POWERPOINT document ==> application/ms-powerpoint"   
PDF document ==> application/pdf"   
ZIP document ==> application/zip"   


arrow
arrow
    全站熱搜

    羅伯特 發表在 痞客邦 留言(0) 人氣()