java根据模板导出功能

/**
     * 监管导出
     *
     * @throws Exception
     */
    @RequestMapping(params = "daoChuJianGuan")
    public void daoChuJianGuan(HttpServletResponse response, HttpServletRequest request) throws Exception {
      List<JianGuanTj> JianGuanTjs =getDataJianGuan(request);
  //设置导出类型
      response.setContentType("application/ms-excel;");
     //获取项目的根目录
    String path = request.getSession().getServletContext().getRealPath("/");
        //根据项目的根目录找到Workbook
        Workbook os = WorkbookFactory.create(new File(path + "template" + File.separator + "excel" + File.separator + "出入境特殊物品单位信息监管统计.xlsx"));
        //获取模板上的sheet,‘统计’为模板的 sheet名称
      Sheet sheet = os.getSheet("统计");
        int jigouseek = 0;
        JianGuanTj top = null;//实体
        for (int i = 0; i < JianGuanTjs.size(); i++) {
        JianGuanTj jianGuanTj = JianGuanTjs.get(i);
        Row row=sheet.createRow(sheet.getLastRowNum() + 1);
        //简单的,不涉及到合并单元格的,注释部分忽略
        // if (top == null) {
          //      jigouseek = row.getRowNum();
          //   }else{
          // if (!(top.getJigou().trim().equals(jianGuanTj.getJigou().trim()))) {
          //  if (jigouseek != row.getRowNum() - 1) {
          //  sheet.addMergedRegion(new CellRangeAddress(jigouseek, row.getRowNum() - 1, 0, 0));
          //     }
          //  jigouseek = row.getRowNum();
          //    }
          // }
        TSDepart tsDepart = (TSDepart) systemService.getEntity(TSDepart.class, jianGuanTj.getJigou());
        if(tsDepart!=null){
        row.createCell(0).setCellValue(tsDepart.getDepartname());
        }else{
        row.createCell(0).setCellValue(jianGuanTj.getJigou());
        }
        row.createCell(1).setCellValue(jianGuanTj.getXiangmu());
        row.createCell(2).setCellValue(jianGuanTj.getHeJiCorpJiaShu());
        row.createCell(3).setCellValue(jianGuanTj.getHeJiCorpJgCiShu());
        row.createCell(4).setCellValue(jianGuanTj.getHeJiNotCorpJiaShu());
        row.createCell(5).setCellValue(jianGuanTj.getHeJiNotCorpJgCiShu());
        row.createCell(6).setCellValue(jianGuanTj.getHxJgXiaoJiJiaShu());
        row.createCell(7).setCellValue(jianGuanTj.getHxJgXiaoJiCiShu());
        row.createCell(8).setCellValue(jianGuanTj.getHxJgNotJiaShu());
        row.createCell(9).setCellValue(jianGuanTj.getHxJgNotCiShu());
        row.createCell(10).setCellValue(jianGuanTj.getRiChJgXiaoJiJiaShu());
        row.createCell(11).setCellValue(jianGuanTj.getRiChJgXiaoJiCiShu());
        row.createCell(12).setCellValue(jianGuanTj.getRiChJgNotJiaShu());
        row.createCell(13).setCellValue(jianGuanTj.getRiChJgNotCiShu());
        top=jianGuanTj;
}
   //设置头部信息
        response.setHeader("Content-disposition", "attachment; filename=" + new String(("出入境特殊物品单位信息监管统计.xlsx").getBytes("GBK"), "ISO8859-1"));
        os.write(response.getOutputStream());
       //关闭流。此处应该用try catch finally ,防止出现异常导致流无法关闭,内存溢出,只是此处省略。使用时切忌使用
        response.getOutputStream().close();
    }
[size=x-small][/size]

猜你喜欢

转载自yuzhouchangwan.iteye.com/blog/2323147