冷冷冷,冻死了

导航

将数据添加到word表格模板的解决方案

1.首先导入依赖
 1         <dependency>
 2             <groupId>cn.afterturn</groupId>
 3             <artifactId>easypoi-base</artifactId>
 4             <version>3.2.0</version>
 5         </dependency>
 6         <dependency>
 7             <groupId>cn.afterturn</groupId>
 8             <artifactId>easypoi-web</artifactId>
 9             <version>3.2.0</version>
10         </dependency>
11         <dependency>
12             <groupId>cn.afterturn</groupId>
13             <artifactId>easypoi-annotation</artifactId>
14             <version>3.2.0</version>
15         </dependency>

2.工具方法编写

    /**
     * 将内容写入word模板导出相应的word文件
     * @param map 数据map用于替换 模板当中的数据
     * @param pathMap 路径map包括输入输出地址,以及图片的地址
     */
    public static void writeWordData(Map<String, Object> map
            , Map<String, String> pathMap) {
        if (map.get("photoPath") != null || "".equals(map.get("photoPath"))) {
            WordImageEntity img = new WordImageEntity();
            img.setHeight(photoHeight);
            img.setWidth(photoWidth);
            img.setUrl(map.get("photoPath").toString());
            img.setType(WordImageEntity.URL);
            map.put("photo", img);
            map.remove("photoPath");
        }
        XWPFDocument xwpfDocument = null;
        OutputStream ops = null;
        try {
            String templatePath = pathMap.get("templatePath");
            xwpfDocument = WordExportUtil.exportWord07(templatePath, map);
            ops = new FileOutputStream(pathMap.get("outFilePath"));
            xwpfDocument.write(ops);
            ops.flush();
            ops.close();
       //该方法用于换行,可以参考我的另一篇博客 //wordNewLine(pathMap.get(
"outFilePath"), pathMap.get("outFilePath")); } catch (Exception e) { logger.error("将数据写入word模板发生异常", e); e.printStackTrace(); } return ; }

测试方法

    public static void main(String[] args) throws FileNotFoundException {

        String outFilePath = "C:\\Users\\NWP\\Desktop\\test\\jyryApproveqwe123.docx";
        String lvli = "11111111111111111111111111111111111111111111111111111111111112311111111" + "\n"
                + "11111110000000000000000000000000000000000000000000000000000000111111111";
        String templatePath = "C:\\Users\\NWP\\Desktop\\test\\jyryApprove.docx";
        Map<String, Object> map = new HashMap<>();
        Map<String, String> pathMapnew = new HashMap<>();
        pathMapnew.put("outFilePath", outFilePath);
        pathMapnew.put("templatePath", templatePath);
        map.put("lvli", lvli);
        writeWordData(map, pathMapnew);
    }

1. 在导入图片地址的时候需要注意一点,文件夹的路径当中不能带有.,如果带有点会导致图片识别不出来

 

 

 

 

posted on 2022-02-19 14:39  沙湖遇雨  阅读(336)  评论(0)    收藏  举报