XWPFTemplate生成根据模板文件填充内容生成word文件

1、查询数据放入Map


    @Override
    public Map<String, Object> generateReport(String id,String areaCode) {
        //查询主数据
        ProCheckSpecialDO p = proCheckSpecialMapper.generateReport(id,areaCode);
        Map<String, Object> map = new HashMap<>();
        map.put("deptName", p.getEleName());
        map.put("columnName", p.getLabel());
        map.put("errorTotal", p.getErrorTotal());
        map.put("title", p.getTitle());
        QueryWrapper qw = new QueryWrapper();
        qw.eq("bind_id",p.getId());
        qw.orderByAsc("create_time");
        //查询所有的关于当前遍历数据错误项
        try {
            List<Map<String, Object>> contentsList = new ArrayList<>();
            List<ProCheckSpecialErrorDO> errorDOS = specialErrorMapper.selectList(qw);
            for(ProCheckSpecialErrorDO error : errorDOS){
                Map<String,Object> content = new HashMap<>();
                content.put("contentText","问题描述:"+error.getContent());
                ArrayList<LinkedHashMap> arrayList=new ArrayList();
                QueryWrapper imgQw = new QueryWrapper();
                imgQw.eq("parent_id",error.getId());
                imgQw.eq("bind_id",p.getId());
                List<ProCheckSpecialErrorImageDO> images = imageMapper.selectList(imgQw);
                for (ProCheckSpecialErrorImageDO i : images) {
                    LinkedHashMap m = new LinkedHashMap();
                    m.put("image", Pictures.ofStream(new FileInputStream(i.getFilePath()), PictureType.PNG)
                            .size(600, 330).create());
                    arrayList.add(m);
                }
                content.put("images",arrayList);
                contentsList.add(content);
            }
            map.put("contents",contentsList);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return map;
    }

2、使用XWPFTemplate将数据填充的模板文件,并且向HttpServletResponse中写入


    @GetMapping("/generateReport")
    @ApiOperation("生成报告")
    public void generateReport(String id, String code, HttpServletResponse response) {
        OutputStream out = null;
        try {
            Map<String, Object> map = proCheckSpecialService.generateReport(id,code);
            String deptName = (String) map.get("deptName");
            out = response.getOutputStream();
            XWPFTemplate template = XWPFTemplate.compile(templatePath).render(map);
            template.write(out);
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition","attachment;filename="+deptName+"报告.docx");
            response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(out != null){
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

3、模板文件

  • {{参数名称}} - 单个属性字段

  • {{?参数名称}}{{=#this}}{{/参数名称}} - 判断是否存在,如果存在则显示(可判断任意类型如集合以及单个属性)

  • {{?集合名称}}{{@参数名称}}{{/集合名称}} - 判断是否存在,如果存在则显示图片

    模板文件:
    XWPFTemplate生成根据模板文件填充内容生成word文件_第1张图片

你可能感兴趣的:(JAVA,Spring,java,开发语言)