blogspot.com-GA4

星期一, 3月 23, 2009

POI 輸出 Excel 檔案 直接讓使用者下載

response.reset() ;
response.setContentType(contenttype);
response.setHeader("Content-Disposition", "attachment;Filename=" + filename + ".xls");

..... 在這邊設定 POI 輸出的 Excel 內容 ......

// 1. 利用網頁的輸出方式,顯示給使用者進行檔案下載的動作
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();

// 2. 設定輸出模式為檔案輸出,輸出至目錄下
FileOutputStream outfile = new FileOutputStream("/tmp/"+ filename +".xls");
workbook.write(outfile);
outfile.close();

沒有留言: