java通过word模板生成word文档

介绍

上次公司项目需要一个生成word文档的功能,有固定的模板根据业务填充数据即可,由于从来没做过,项目也比较着急于是去网上找有没有合适的工具类,找了好几种,看到其中有freeMark模板生成比较靠谱于是采用这个,正常生成成功了还挺高兴的于是修改优化部署测试,出问题了,由于我一直使用wps可以正常打开,但是同事使用office打不开,于是各种查找原因都没好,于是只能转变思路又试了两种还是不好用,直到发现这款模板生成 poi-tl 真的做的很不错,而且是国人写的,关于学习这个东西还是看官方文档的好。
文档地址: http://deepoove.com/poi-tl

源码

源码查看规则
源码位置: blog-study:static-utils - word
在源码里可以完整的操作

代码展示

public static void main(String[] args) {
        //模板、文件、图片路径
        String workPath=System.getProperty("user.dir") + "/static-utils/src/main/resources/word/";
        String templateName="test.docx";
        Map<StringObject> datas = new HashMap<StringObject>() {
            {
                //文本
                put("name","xiaoguo");
                put("sex","男");
                put("year","20200105");
                put("hello","xiaoguo写于2020年一月");

                //自定义表格
                RowRenderData header = RowRenderData.build(new TextRenderData("1C86EE""姓名"), new TextRenderData("1C86EE""学历"));
                RowRenderData row0 = RowRenderData.build("张三""研究生");
                RowRenderData row1 = RowRenderData.build("李四""博士");
                RowRenderData row2 = RowRenderData.build("王五""博士后");
                put("tables"new MiniTableRenderData(header, Arrays.asList(row0, row1, row2)));

                //自定义有序列表
                put("testText"new NumbericRenderData(NumbericRenderData.FMT_DECIMAL, new ArrayList<TextRenderData>() {
                    {
                        add(new TextRenderData("Plug-in grammar"));
                        add(new TextRenderData("Supports word text, header..."));
                        add(new TextRenderData("Not just templates, but also style templates"));
                    }
                }));

                //网落图片
                put("picture"new PictureRenderData(200150".jpg", BytePictureUtils.getUrlBufferedImage("https://gss3.bdstatic.com/7Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=61c551093f6d55fbd1cb7e740c4b242f/d8f9d72a6059252d937820d3369b033b5ab5b9fd.jpg")));
                //本地图片
                put("picture2"new PictureRenderData(200150".jpg", BytePictureUtils.getLocalByteArray(new File(workPath + "c1.jpg"))));

            }
        };

        generateWord(datas, workPath + templateName, workPath);
    }


    /**
     * 通过word模板并生成word文档
     *
     * @param paramData    参数数据
     * @param templatePath word模板地址加模板文件名字
     * @param outFilePath  输出文件地址(不带文件名字)
     * @return 生成的word文件
     */

    public static File generateWord(Map<StringObject> paramData, String templatePath, String outFilePath) {
        String outFileName = "word_" + System.currentTimeMillis() + "_" + random.nextInt(100) + ".doc";
        return generateWord(paramData, templatePath, outFilePath, outFileName);
    }


    /**
     * 通过word模板并生成word文档
     *
     * @param paramData    参数数据
     * @param templatePath word模板地址加模板文件名字
     * @param outFilePath  输出文件地址(不带文件名字)
     * @param outFileName  输出文件名字
     * @return 生成的word文件
     */

    public static File generateWord(Map<StringObject> paramData, String templatePath, String outFilePath, String outFileName) {
        //判断输出文件路径和文件名是否含有指定后缀
        outFilePath = CommonUtil.addIfNoSuffix(outFilePath, "/""\\");
        outFileName = CommonUtil.addIfNoSuffix(outFileName, ".doc"".docx");
        //解析word模板
        XWPFTemplate template = XWPFTemplate.compile(templatePath).render(paramData);
        //输出文件
        FileOutputStream out = null;
        File outFile = new File(outFilePath + outFileName);
        try {
            out = new FileOutputStream(outFile);
            template.write(out);
            out.flush();
        } catch (IOException e) {
            log.error("生成word写入文件失败", e);
        } finally {
            if (template != null) {
                try {
                    template.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return outFile;
    }

猜你喜欢

转载自www.cnblogs.com/chunyun/p/12153085.html