部落客廣告聯播

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程式:




<%@ page language="java" contentType="text/html; charset=BIG5" pageEncoding="BIG5"%>
<%@page import="org.apache.poi.hssf.usermodel.*,org.apache.poi.hssf.util.*,java.io.*" autoFlush="true" buffer="10240kb"%>
<%@taglib uri="report" prefix="report" %>
<%

response.setHeader( "Content-disposition" , "attachment; filename=ProjectScorecardReport.xls" );
response.setContentType("application/vnd.ms-excel");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);


HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("MajorScorecardYearReport");

//..... more here

wb.write(response.getOutputStream());
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
%>



而我找到的答案告訴我,要解決這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:
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:




<%@ page language="java" contentType="text/html; charset=BIG5" pageEncoding="BIG5"%>
<%@page import="org.apache.poi.hssf.usermodel.*,org.apache.poi.hssf.util.*,java.io.*" autoFlush="true" buffer="10240kb"%>
<%@taglib uri="report" prefix="report" %>
<%

response.setHeader( "Content-disposition" , "attachment; filename=ProjectScorecardReport.xls" );
response.setContentType("application/vnd.ms-excel");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);


HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("MajorScorecardYearReport");

//..... more here

wb.write(response.getOutputStream());
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
%>


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.

沒有留言: