前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >快速学习POI-模板打印

快速学习POI-模板打印

作者头像
cwl_java
发布2020-02-11 13:24:22
7540
发布2020-02-11 13:24:22
举报
文章被收录于专栏:cwl_Java

1 模板打印

1.1 概述

自定义生成Excel报表文件还是有很多不尽如意的地方,特别是针对复杂报表头,单元格样式,字体等操作。手写这些代码不仅费时费力,有时候效果还不太理想。那怎么样才能更方便的对报表样式,报表头进行处理呢?答案是使用已经准备好的Excel模板,只需要关注模板中的数据即可。

1.2 模板打印的操作步骤

  1. 制作模版文件(模版文件的路径)
  2. 导入(加载)模版文件,从而得到一个工作簿
  3. 读取工作表
  4. 读取行
  5. 读取单元格
  6. 读取单元格样式
  7. 设置单元格内容
  8. 其他单元格就可以使用读到的样式了

1.3 代码实现

代码语言:javascript
复制
    @RequestMapping(value = "/export/{month}", method = RequestMethod.GET)
    public void export(@PathVariable(name = "month") String month) throws Exception {
		//1.构造数据
        List<EmployeeReportResult> list =
                userCompanyPersonalService.findByReport(companyId,month+"%");
		//2.加载模板流数据
        Resource resource = new ClassPathResource("excel-template/hr-demo.xlsx");
        FileInputStream fis = new FileInputStream(resource.getFile());
		//3.根据文件流,加载指定的工作簿
        XSSFWorkbook wb = new XSSFWorkbook(fis);
		//4.读取工作表
        Sheet sheet = wb.getSheetAt(0);
		//5.抽取公共的样式
        Row styleRow = sheet.getRow(2);
		CellStyle [] styles = new CellStyle[styleRow.getLastCellNum()];
        for(int i=0;i<styleRow.getLastCellNum();i++) {
            styles[i] = styleRow.getCell(i).getCellStyle();
       	}
        //6.构造每行和单元格数据
        AtomicInteger datasAi = new AtomicInteger(2);
        Cell cell = null;
        for (EmployeeReportResult report : list) {
            Row dataRow = sheet.createRow(datasAi.getAndIncrement());
            //编号
            cell = dataRow.createCell(0);
            cell.setCellValue(report.getUserId());
            cell.setCellStyle(styles[0]);
            //姓名
            cell = dataRow.createCell(1);
            cell.setCellValue(report.getUsername());
            cell.setCellStyle(styles[1]);
            //手机
            cell = dataRow.createCell(2);
            cell.setCellValue(report.getMobile());
            cell.setCellStyle(styles[2]);
            //最高学历
            cell = dataRow.createCell(3);
            cell.setCellValue(report.getTheHighestDegreeOfEducation());
            cell.setCellStyle(styles[3]);
            //国家地区
            cell = dataRow.createCell(4);
            cell.setCellValue(report.getNationalArea());
            cell.setCellStyle(styles[4]);
            //护照号
            cell = dataRow.createCell(5);
            cell.setCellValue(report.getPassportNo());
            cell.setCellStyle(styles[5]);
            //籍贯
            cell = dataRow.createCell(6);
            cell.setCellValue(report.getNativePlace());
            cell.setCellStyle(styles[6]);
            //生日
            cell = dataRow.createCell(7);
            cell.setCellValue(report.getBirthday());
            cell.setCellStyle(styles[7]);
            //属相
            cell = dataRow.createCell(8);
            cell.setCellValue(report.getZodiac());
            cell.setCellStyle(styles[8]);
            //入职时间
            cell = dataRow.createCell(9);
            cell.setCellValue(report.getTimeOfEntry());
            cell.setCellStyle(styles[9]);
            //离职类型
            cell = dataRow.createCell(10);
            cell.setCellValue(report.getTypeOfTurnover());
            cell.setCellStyle(styles[10]);
            //离职原因
            cell = dataRow.createCell(11);
            cell.setCellValue(report.getReasonsForLeaving());
            cell.setCellStyle(styles[11]);
            //离职时间
            cell = dataRow.createCell(12);
            cell.setCellStyle(styles[12]);
            cell.setCellValue(report.getResignationTime());
       }
        String fileName = URLEncoder.encode(month+"人员信息.xlsx", "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("ISO8859-1")));
        response.setHeader("filename", fileName);
        wb.write(response.getOutputStream());
   }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 模板打印
    • 1.1 概述
      • 1.2 模板打印的操作步骤
        • 1.3 代码实现
        相关产品与服务
        腾讯云 BI
        腾讯云 BI(Business Intelligence,BI)提供从数据源接入、数据建模到数据可视化分析全流程的BI能力,帮助经营者快速获取决策数据依据。系统采用敏捷自助式设计,使用者仅需通过简单拖拽即可完成原本复杂的报表开发过程,并支持报表的分享、推送等企业协作场景。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档