JAVA使用POI-TI根据word模板导出word

JAVA使用POI-TI根据word模板导出word

前后端框架使用的是JeecgBoot+antv 代码写的不太好,还请大佬手下留情
导出后的文档样图(包含了table、多选框)
在这里插入图片描述

1.先将准备好的文档修改
在这里插入图片描述
每条数据对应的字段使用{{}}花括号包裹,
!!!记住,括号里面不能有空格。 记住,括号里面不能有空格。记住,括号里面不能有空格

2.将模板放入文件夹下,并在yml配置文件配置路径

test-template: D:\项目文档\06-试验任务通知书.docx

3.前端点击事件
在这里插入图片描述
this.dataSource是table列表的数据

4.后端接口实现

 @Override
    public void downWord(ProductionTestVo productionTestVo, HttpServletResponse response) {
        //查询实施规则
        if (productionTestVo.getImplementingRules()!=null){
            List<String> stringList = prodSysProductMapper.selectListImple(Arrays.asList(productionTestVo.getImplementingRules().split(",")));
            productionTestVo.setImplementingRules(Joiner.on(",").join(stringList));
        }


        if (productionTestVo.getModelId()!=null){
            List<String> listModel = prodSysProductMapper.selectListModel(Arrays.asList(productionTestVo.getModelId().split(",")));
            productionTestVo.setModelId(Joiner.on(",").join(listModel));
        }

        Map<String, Object> datas = new HashMap<String, Object>();
        XWPFTable xwpfTable =null;
        try {
            ConfigureBuilder builder = Configure.builder();
            builder.useSpringEL(false);
            // 表格内内容
            List<Map<String, String>> tableList = new ArrayList<>();
            datas.put("oneTable",tableList );
            //前端必须使用JSON.stringify将对象转换
            List<ProductInformation> productInformations = JSON.parseArray(productionTestVo.getInfos(),ProductInformation.class);
            for (ProductInformation productInformation : productInformations) {
                Map<String, String> map = new HashMap<>();
                map.put("sampleType",productInformation.getSampleType());
                map.put("auditRange",productInformation.getAuditRange());
                map.put("productionSubcategory",productInformation.getProductionSubcategory());
                map.put("productSpecsType",productInformation.getProductSpecsType());
                map.put("samplePrimary",String.valueOf(productInformation.getSamplePrimary()));
                tableList .add(map);
            }
            //选中
            TextRenderData textRenderData = new TextRenderData("\u2611", new Style("MS Gothic", 12));
            //未选中
            TextRenderData unCancel = new TextRenderData("\u2610", new Style("MS Gothic", 12));
            //对象转map
            TestCheckVo checkVo = JSON.parseObject(JSON.toJSONString(productionTestVo), TestCheckVo.class);
            //认证领域  1自愿性产品认证	  2强制性产品认证
            if ("1".equals(productionTestVo.getTypeBusiness())){
                checkVo.setConstraint(textRenderData);
            }else{
                checkVo.setConstraint(unCancel);
            }
            if ("2".equals(productionTestVo.getTypeBusiness())){
                checkVo.setVolunteer(textRenderData);
            }else{
                checkVo.setVolunteer(unCancel);
            }

            //检测类别 0新申请 1变更	 2监督 3非例行抽样检测	4按新标准确认	5其他
            //如果是新申请
            String[] temp;
            temp = productionTestVo.getDetectCatetory().split(",");
            ArrayList<String> list = new ArrayList<>(Arrays.asList(temp));
            HashSet<String> hs = new HashSet<>();
            hs.addAll(list);

            if (hs.contains("0")){
                checkVo.setNewApplication(textRenderData);
            }else{
                checkVo.setNewApplication(unCancel);
            }
            if (hs.contains("1")){
                checkVo.setSupervision(textRenderData);
            }else{
                checkVo.setSupervision(unCancel);
            }
            if (hs.contains("2")){
                checkVo.setAlteration(textRenderData);
            }else{
                checkVo.setAlteration(unCancel);
            }
            if (hs.contains("3")){
                checkVo.setNoSample(textRenderData);
            }else{
                checkVo.setNoSample(unCancel);
            }
            if (hs.contains("4")){
                checkVo.setNewAffirm(textRenderData);
            }else{
                checkVo.setNewAffirm(unCancel);
            }
            if (hs.contains("5")){
                checkVo.setElseOth(textRenderData);
            }else{
                checkVo.setElseOth(unCancel);
            }

            //检测项目 0客户送样检测	 1项目现场检测
            if ("0".equals(productionTestVo.getCheckType())){
                checkVo.setCustomerSam(textRenderData);
            }else{
                checkVo.setCustomerSam(unCancel);
            }
            if ("1".equals(productionTestVo.getCheckType())){
                checkVo.setProjectField(textRenderData);
            }else{
                checkVo.setProjectField(unCancel);
            }

            //检测方式   0全项 1部分项目
            if ("0".equals(productionTestVo.getTestItemStatus())){
                checkVo.setAll(textRenderData);
            }else{
                checkVo.setAll(unCancel);
            }
            if ("1".equals(productionTestVo.getTestItemStatus())){
                checkVo.setPart(textRenderData);
            }else{
                checkVo.setPart(unCancel);
            }

            //检测后样品处置   0检测机构自行处置 1退回企业(需企业负责运输费用),填写收件地址
            if ("0".equals(productionTestVo.getDisposalAfterTest())){
                checkVo.setDiscretion(textRenderData);
            }else{
                checkVo.setDiscretion(unCancel);
            }
            if ("1".equals(productionTestVo.getDisposalAfterTest())){
                checkVo.setReturnEnterprise(textRenderData);
            }else{
                checkVo.setReturnEnterprise(unCancel);
            }
            datas.put("checkVo",checkVo);
            // 此处使用了poi-tl的<表格行循环插件>,此处一定要进行参数bind,方便word模板参数替换
            LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
            Configure build = Configure.builder().bind("oneTable", policy).build();
            XWPFTemplate template = XWPFTemplate.compile(testTemplPath,build).render(datas);
            String fileName = testTemplPath.substring(testTemplPath.lastIndexOf(File.separator) + 1);
            response.setContentType("application/force-download");
            response.addHeader("content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
            ServletOutputStream out = response.getOutputStream();
            template.write(out);
            response.flushBuffer();
            out.flush();
            out.close();
            PoitlIOUtils.closeQuietlyMulti(template);
        } catch (Exception e) {
            log.error("下载文件失败" + e.getMessage());
            response.setStatus(404);
            e.printStackTrace();
        }


    }

多选框实体类

@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class TestCheckVo extends ProductionTestVo{


    /**
     * 强制性产品认证
     */
    private TextRenderData constraint;
    /**
     * 自愿性产品认证
     */
    private TextRenderData volunteer;

    /**
     * 新申请
     */
    private TextRenderData newApplication;
    /**
     * 监督
     */
    private TextRenderData supervision;

    /**
     * 变更
     */
    private TextRenderData alteration;
    /**
     * 非例行抽样检测
     */
    private TextRenderData noSample;

    /**
     * 按新标准确认
     */
    private TextRenderData newAffirm;

    /**
     * 其他
     */
    private TextRenderData elseOth;

    /**
     * 全部
     */
    private TextRenderData all;

    /**
     * 部分
     */
    private TextRenderData part;


    /**
     * 客户送样检测
     */
    private TextRenderData customerSam;

    /**
     * 项目现场检测
     */
    private TextRenderData projectField;

    /**
     * 检测机构自行处置
     */
    private TextRenderData discretion;

    /**
     * 退回企业(需企业负责运输费用)
     */
    private TextRenderData returnEnterprise;

5.效果图
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值