故事起源於我要使用POI輸出報表,原有的報表乃是用JSP+ jsp custom tag來產生的。
當然用POI輸出XLS檔,使用Servlet當然是首選,這在網路上許多前輩也常提,但總不是很深切的了解為何?
由於許多報表運算邏輯都放在custom tag裡,故愚笨的我選擇為了能共用tag所以在JSP中配合custom tag和script let來產生XLS檔,但前前後後遇到了不少問題。
在單純的JSP(不包含EL及custom tag狀況)中,來處理要輸出的Stream若遇到狀況,似乎還不難解決,但若JSP中包含了custom tag就複雜多了,況且有些問題再script let強加補強就算能用,移到了別的container還是有不能跑的可能,這涉及到各個container將JSP轉譯java servlet的實做程式不同。
首先是JSP buffer的問題,我們知道當JSP寫到Response的資料到達buffer值時即會flush到客戶端去(假設autoFlush=true),每個container預設的JSP buffer size不同。
然而在JSP裡要將二元資料寫到串流前,通常都要將先前JSP轉譯成java後裡頭的out.print(" \t\n");之類的東西清掉,才不會在資料頭出現奇奇怪怪的東西,所需要使用out.clear() 之類的做清除的動作,但如果在執行out.clear()之前buffer就已經被flush過了,那麼就會IOException,不要的out.print(" \t\n");也早就被輸出了,為了這個問題,可以透過JSP的Directive來將buffer設大點,暫時解決。
第二,在我的JSP最後段是workbook.write(response.getOutputStream); response.flush(); 但在Tomcat中執行,出現了錯誤,由於Tomcat算是廣為使用,到處有人可問,所以不用一天的時間我就早到了解決方法,在我的JSP最後再加上 out=response.pushBody( );就解決了。打開java檔查看乃是Tomcat的實作在jsp頁面後會再對out做處理,但我們提前將整個response給flush掉了,所以只要再push一個新的JspWriter給out就能解決了。
第三,當程式移植到了Resin時,問題就來的,輸出的Excel檔會是一堆空白,然後再活頁簿往下拉發現許多亂碼。同樣的我們將該JSP頁面經由Resin轉譯成的servlet來觀察問題所在,發現Resin實做會將每個TagSupport的endTag()後方加上out.flush( );[相對Tomcat沒有],故會在之前就輸出許多的空白和escape character,為了解決這個問題,於是將custom tag由TagSupport改用BodyTagSupport來實做,並且在JSP頁面上每一個custom tag結尾加上pageContext.clear( );
,恩,又是暫時解決!!
第四,程式到了另一個或許你也沒聽過的container - Novell Silver Stream,好玩,輸出的XLS完全空白,同樣觀察java檔,然而,到現在對於這問題還是一無所獲!! 無法解決......
上面寫了這麼大串,或許那都不重要,但我卻真正體會不要再JSP中處理Stream的意義了!!
恩... 好樣,最後還是要用servlet寫...........................
( 哭 ) 浪費我好多時間阿...................................................
部落客廣告聯播
2007年7月24日 星期二
2007年7月18日 星期三
JSP中 關於IE6開啟附件時的問題(A problem about direct open excel file from IE won't woks)
故事是這樣的:
工作上使用POI將資料寫出讓user下載,當Excel寫出response後,IE會提示使用者「開啟」或是「儲存」檔案,問題就在於使用者按下儲存正常順利,但若按下開啟,便會出現如下圖示:

好笑的是,用FireFox不會,聽說用IE7也不會,那麼算是IE6的Bug囉!!
又跑去Google神殿拜神,滿地的預測籤詩讓我找到了答案。
以下是我的原始JSP程式:
而我找到的答案告訴我,要解決這bug非常簡單,只要多設幾個http header即可:
把這三行程式加入後,果然成功了!
不管PHP或ASP或其他CGI語言,都可用相同方法設定Header即可解決,因為問題出在IE!
================================================================
It's a short story:
I need to export reports to Excel xls files by using POI library in my JSP page.
When everything went on well, there was some problem when user who was using IE6 will get an error message when they directly opened XLS file, like thihs:
And it won't appear if the user's browser is FireFox or IE7. So,it is a bug of IE6 baldly.
After searching through Google,I got a useful solution to solve this bug.
Below is my original JSP file:
It's really simple to solve this bug , you just need to add three lines of code to set related http headers:
After Adding these codes , It works well without any problem.
And you can use the same method to set these three header to resolve the problem in PHP or ASP. Because this problem is a Bug of IE6.
工作上使用POI將資料寫出讓user下載,當Excel寫出response後,IE會提示使用者「開啟」或是「儲存」檔案,問題就在於使用者按下儲存正常順利,但若按下開啟,便會出現如下圖示:
好笑的是,用FireFox不會,聽說用IE7也不會,那麼算是IE6的Bug囉!!
又跑去Google神殿拜神,滿地的預測籤詩讓我找到了答案。
以下是我的原始JSP程式:
|
而我找到的答案告訴我,要解決這bug非常簡單,只要多設幾個http header即可:
response.setDateHeader("Expires", 0);
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "max-age=0");
把這三行程式加入後,果然成功了!
不管PHP或ASP或其他CGI語言,都可用相同方法設定Header即可解決,因為問題出在IE!
================================================================
It's a short story:
I need to export reports to Excel xls files by using POI library in my JSP page.
When everything went on well, there was some problem when user who was using IE6 will get an error message when they directly opened XLS file, like thihs:
After searching through Google,I got a useful solution to solve this bug.
Below is my original JSP file:
|
It's really simple to solve this bug , you just need to add three lines of code to set related http headers:
response.setDateHeader("Expires", 0);
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "max-age=0");
After Adding these codes , It works well without any problem.
And you can use the same method to set these three header to resolve the problem in PHP or ASP. Because this problem is a Bug of IE6.
標籤:
attachment,
bug,
excel,
http header,
IE,
jsp,
response,
xls
2007年7月4日 星期三
Java存取MS Office檔案格式函式庫Apache POI (Java library for Accessing MS Office format files)
Java存取MS Office檔案格式函式庫Apache POI
(Java library for Accessing MS Office format files)
最常被用來存取Excel XLS檔案,
此外還有提供對Word及power point及visio檔案格式有限的存取,
請見官方網站。
使用文件可在此取得。(建議閱讀)
Normally used for accessing Excel XLS files,
and have the limited ability to access Word,power point and visio files,
see Official Site here
and documents here
(Java library for Accessing MS Office format files)
最常被用來存取Excel XLS檔案,
此外還有提供對Word及power point及visio檔案格式有限的存取,
請見官方網站。
使用文件可在此取得。(建議閱讀)
Normally used for accessing Excel XLS files,
and have the limited ability to access Word,power point and visio files,
see Official Site here
and documents here
訂閱:
文章 (Atom)