poi reads the template, populate the template data, the export template modification

public  void to_exportAllAssess () { 
the ArrayList
<AssessResultDetail> = assessResultDetailList query data ready to be filled excel HSSFWorkbook Workbook = AssessExportUtil.exportAllAssess (assessResultDetailList); the try { // the response to the client the this .setResponseHeader (Response, assessTime.getName () + "Statistics table * .xls " ); the OutputStream OS = response.getOutputStream (); workbook.write (OS); os.flush (); os.close (); } the 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 a java.io.FileInputStream;
 Import of java.util.ArrayList; 

Import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 Import org.apache.struts2.ServletActionContext;
 Import .. COM AssessResultDetail *; 

public  class AssessExportUtil {
     / * 
    HSSFWorkbook: operating Excel2003 past (2003) version of the extension is * .xls 
    XSSFWorkbook: Excel2007 operating version of the extension is .xlsx * / 
    public  static HSSFWorkbook exportAllAssess (the ArrayList <AssessResultDetail> assessResultDetailList) {
         // Excel template path
        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; } }

 

Guess you like

Origin www.cnblogs.com/dreamShines/p/11505565.html