jxls根據模板匯出excel

java_陳序猿發表於2017-06-29
public class ExportExcel{
HttpServletRequest request;
HttpServletResponse response;
List saff;
String fileName;
public ExportExcel(HttpServletRequest request, HttpServletResponse response, List saff, String fileName ){
this.request=request;
this.response=response;
this.saff=saff;
this.fileName=fileName;
}
public void exportProject(String sourceName)throws Exception{
Map<String,Object> map=new HashMap<>();
map.put("fileName",fileName);
map.put("testforms",saff);
//獲得模板路徑
String path=request.getServletContext().getRealPath("excel_export");
String wjpath=path+ File.separator+sourceName;
//準備輸出流
OutputStream os=null;
try {
//設定響應頭
response.setContentType("application/x-excel");
//response.setHeader("Content-Disposition", "attachment;filename=studentInfo.xls");
response.setHeader("Content-Disposition", "attachment;filename="+new String((fileName+".xls").getBytes(),"iso8859-1"));
response.setContentType("application/ms-excel");
os = response.getOutputStream();
XLSTransformer transformer = new XLSTransformer();
try {
//獲得模板的輸入流
InputStream in = new BufferedInputStream(new FileInputStream(wjpath));
//將beans通過模板輸入流寫到workbook中
Workbook workbook = transformer.transformXLS(in, map);
//將workbook中的內容用輸出流寫出去
workbook.write(os);
os.flush();

}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
} catch (Exception e) {
e.printStackTrace();
}
}

}


demo下載地址  http://download.csdn.net/download/maple980326/9959219

相關部落格地址: http://blog.csdn.net/yuliqi0429/article/details/41907711

相關文章