poi读取模板,填充模板数据,导出修改后的模板

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 org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.*.AssessResultDetail;

public class AssessExportUtil {
    /*
    HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls
    XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
    SXSSFWorkbook : 百万数据报表,扩展名用.xlsx,不支持模板打印
    */
    public static HSSFWorkbook exportAllAssess(ArrayList<AssessResultDetail> assessResultDetailList) {
        //加载模板
        //Resource resource = new ClassPathResource("template/assessValue.xls");
        //FileInputStream fis = new FileInputStream(resource.getFile());
        //根据模板创建工作簿
        //Workbook wb = new HSSFWorkbook(fis);
        
        //excel模板路径 struts2获取
        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;
    }
    
}

 

posted @ 2019-09-11 12:15  lost_s  阅读(1514)  评论(0编辑  收藏  举报