public void to_exportAllAssess(){
ArrayList<AssessResultDetail> assessResultDetailList = 查询准备填充excel的数据 HSSFWorkbook workbook = AssessExportUtil.exportAllAssess(assessResultDetailList); try { //响应到客户端 this.setResponseHeader(response, assessTime.getName()+"统计表.xls"); OutputStream os = response.getOutputStream(); workbook.write(os); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } } //发送响应流方法 public void setResponseHeader(HttpServletResponse response, String fileName) { try { try { fileName = new String(fileName.getBytes(),"ISO8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setContentType("application/octet-stream;charset=ISO8859-1"); response.setHeader("Content-Disposition", "attachment;filename="+ fileName); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); } catch (Exception ex) { ex.printStackTrace(); } }
package com.*.util; import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.struts2.ServletActionContext; import com.*.AssessResultDetail; public class AssessExportUtil { /* HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx*/ public static HSSFWorkbook exportAllAssess(ArrayList<AssessResultDetail> assessResultDetailList) { //excel模板路径 File file = new File(ServletActionContext.getServletContext().getRealPath("/WEB-INF/templet/"), "assessValue.xls"); HSSFWorkbook workbook = null; if(file.exists()) { //文件存在 try {//创建workbook workbook = new HSSFWorkbook(new FileInputStream(file));
//TODO 数据填充excel }catch(Exception e){ e.printStackTrace(); } } return workbook; } }