jfinal之使用模板导出excel文件

来源:互联网 发布:js判断是否有class 编辑:程序博客网 时间:2024/06/04 18:18

本文使用java Jfinal快速框架,使用模板导出excel文件:

主要代码如下:

    public void toExcel() throws IOException, InvalidFormatException {        SimpleDateFormat time = new SimpleDateFormat("yyyy年");        String fileName = time.format(new Date())+"母猪保险分户标的投保清单";        String filePath = PathKit.getWebRootPath() + File.separator + "download" + File.separator + fileName+ ".xlsx";        //excel模板路径          File fi = new File(PathKit.getWebRootPath() + File.separator + "tpl" + File.separator + "模板.xlsx");        //读取excel模板          XSSFWorkbook wb = new XSSFWorkbook(fi);        //读取了模板内所有sheet内容          XSSFSheet sheet = wb.getSheet("投保清单模板");        //在相应的单元格进行赋值          XSSFCell title_cell = sheet.getRow(0).getCell(0);        title_cell.setCellValue(fileName);//        college_sheet.addMergedRegion(new CellRangeAddress(9,(short)9,5,(short)11));//指定合并区域//        XSSFCell semester_name_cell = college_sheet.getRow(9).getCell(5);        XSSFRow row;        for (int i=0;i< 10;i++) {            row = sheet.createRow(i+3);            // 第四步,创建单元格,并设置值              row.createCell((short)0).setCellValue("第"+i+"行第一列数据");              row.createCell((short)1).setCellValue("第"+i+"行第二列数据");              row.createCell((short)2).setCellValue("第"+i+"行第三列数据");           }        try ( //修改模板内容导出新模板            FileOutputStream out = new FileOutputStream(filePath)) {            wb.write(out);            renderFile(new File(filePath));//导出excel        } catch (Exception e) {            e.getMessage();        }    }