目 录CONTENT

文章目录

Word模板导出时插入复选框

javalx
2025-01-09 / 0 评论 / 0 点赞 / 207 阅读 / 0 字

关于Word模板导出时插入复选框记录

需求背景

甲方word模板导时需要用到复选模式

word模板如下:

1736416424313

解决方案

将选中和未选中复选框以字体符号的形式放入模板变量中,然后根据判断写入模板中

  • 依赖配置

    <poi.version>4.1.2</poi.version>
    <poi-tl.version>1.10.0</poi-tl.version>
    <!-- 导出工具 -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>${poi.version}</version>
    </dependency>
    
    <!-- word模板工具 -->
    <dependency>
        <groupId>com.deepoove</groupId>
        <artifactId>poi-tl</artifactId>
        <version>${poi-tl.version}</version>
    </dependency>
    
  • java代码

    // Controller
    
        @ApiOperation("导出")
        @ApiOperationSupport(order = 6)
        @PostMapping("/export/{id}")
        @Log(title = "应急演练实施方案-基本情况", businessType = BusinessType.EXPORT)
        @PreAuthorize("@auth.hasPermi('ers:drillActualize:export')")
        public void export(HttpServletResponse response, @PathVariable("id") Long id) throws IOException {
            InputStream inputStream = ImageUtils.getOssStream("ucd-oss-priv/template/word/ers_drill_actualize.docx");
            DrillActualizeExportVO vo = drillActualizeBaseService.getExportById(id);
            Configure configure = Configure.builder().useSpringEL().build();
            // 字体Wingdings 2的符号(渲染出的word复选框可修改)
    //        TextRenderData selectSymbol = new TextRenderData("\u0052",new Style("Wingdings 2",12));
    //        TextRenderData unSelectSymbol = new TextRenderData("\u00A3",new Style("Wingdings 2",12));
            // 其他字体((渲染出的word复选框不可修改))
            //选中
            TextRenderData selectSymbol = new TextRenderData("\u2611", new Style("MS Gothic", 14));
            //未选中
            TextRenderData unSelectSymbol = new TextRenderData("\u2610", new Style("MS Gothic", 14));
            XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                    new HashMap<String, Object>() {
                        {
                            put("vo", vo);
                            put("selectSymbol",  selectSymbol);
                            put("unSelectSymbol",  unSelectSymbol);
                        }
                    }
            );
            template.writeAndClose(response.getOutputStream());
        }
    
  • 导出模板

    1736416964597

0

评论区