小工具知识网

当前位置: 首页 > 行业知识 > java 根据模板导出word文档(poi-tl)

行业知识

java 根据模板导出word文档(poi-tl)

2026-12-04 16:09:17 来源:互联网转载

poi-tl是什么

poi-tl是一个基于Apache POI的Word模板引擎,也是一个免费开源的Java类库,你可以非常方便的加入到你的项目中,并且拥有着让人喜悦的特性。

为什么选择poi-tl

方案移植性功能性易用性
Poi-tlJava跨平台Word模板引擎,基于Apache POI,提供更友好的API低代码,准备文档模板和数据即可
Apache POIJava跨平台Apache项目,封装了常见的文档操作,也可以操作底层XML结构文档不全,这里有一个教程:Apache POI Word快速入门
FreemarkerXML跨平台仅支持文本,很大的局限性不推荐,XML结构的代码几乎无法维护
OpenOffice部署OpenOffice,移植性较差-需要了解OpenOffice的API
HTML浏览器导出依赖浏览器的实现,移植性较差HTML不能很好的兼容Word的格式,样式糟糕-
Jacob、winlibWindows平台-复杂,完全不推荐使用

引入依赖

    com.deepoove    poi-tl    1.10.0

注意,poi-tl是基于poi的,如还引入poi需要注意版本

怎么使用

下面介绍一下poi-tl的几种常见使用方法

1、文本

标签:{{text}}

数据模型:

  • String :文本

  • TextRenderData :有样式的文本

  • HyperlinkTextRenderData :超链接和锚点文本

  • Object :调用 toString() 方法转化为文本

    模板:

     

    代码示例:

        public static void main(String[] args) {        try {            /*文本*/            Map map = new HashMap();            map.put("name", "Sayi");            map.put("author", new TextRenderData("000000", "Sayi"));            map.put("link", new HyperlinkTextRenderData("website", "http://deepoove.com"));            map.put("anchor", new HyperlinkTextRenderData("anchortxt", "anchor:appendix1"));            XWPFTemplate.compile("D:\testin.docx").render(map).writeToFile("D:\1.docx");        } catch (IOException e) {            e.printStackTrace();        }    }}

     效果:

    2、图片

    标签:{{@var}}

    数据模型:

    • String :图片url或者本地路径,默认使用图片自身尺寸

    • PictureRenderData

    • ByteArrayPictureRenderData

    • FilePictureRenderData

    • UrlPictureRenderData

      模板:

      代码示例:

       public static void main(String[] args) {        try {            Map map = new HashMap();                        /*图片*/            map.put("image", "D:\pic.jpg");       // 指定图片路径            map.put("svg", "https://profile-avatar.csdnimg.cn/840f4549a5ae4144b243b203900b98c5_qq_38860250.jpg");         // svg图片            map.put("image1", Pictures.ofLocal("D:\pic.jpg").size(120, 120).create());            // 设置图片宽高            map.put("streamImg", Pictures.ofStream(new FileInputStream("D:\pic.jpg"), PictureType.JPEG)         // 图片流                    .size(100, 120).create());            map.put("urlImg", Pictures.ofUrl("https://profile-avatar.csdnimg.cn/840f4549a5ae4144b243b203900b98c5_qq_38860250.jpg")                    .size(100, 100).create());            // 网络图片(注意网络耗时对系统可能的性能影响)            XWPFTemplate.compile("D:\testin.docx").render(map).writeToFile("D:\2.docx");        } catch (IOException e) {            e.printStackTrace();        }    }

      效果:

       3、表格

      标签:{{#tableTest}}

      数据模型:

      • TableRenderData

        推荐使用工厂 `Tables` 、 `Rows` 和 `Cells` 构建表格模型。

        模板:

        代码示例:

            public static void main(String[] args) {        try {            Map map = new HashMap();            /*表格*/            RowRenderData row0 = Rows.of("姓名", "学历").textColor("FFFFFF")                    .bgColor("4472C4").center().create();            RowRenderData row1 = Rows.create("李四", "博士");            map.put("tableTest", Tables.create(row0, row1));            XWPFTemplate.compile("D:\testin.docx").render(map).writeToFile("D:\3.docx");        } catch (IOException e) {            e.printStackTrace();        }    }

         效果:

         4、列表

        标签:{{*var}}

        数据模型:

        • List
        • NumberingRenderData

          推荐使用工厂 `Numberings` 构建列表模型。

          模板:

           代码示例:

              public static void main(String[] args) {        try {            Map map = new HashMap();            /*列表*/            map.put("listTest", Numberings.create("Plug-in grammar",                    "Supports word text, pictures, table...",                    "Not just templates"));             XWPFTemplate.compile("D:\testin.docx").render(map).writeToFile("D:\4.docx");        } catch (IOException e) {            e.printStackTrace();        }    }

          效果:

          5、嵌套

          标签:{{+nested}}

          数据模型:

          • DocxRenderData

            推荐使用工厂 `Includes` 构建嵌套模型。

            模板:

             

            嵌套子模板:

            代码示例:

            /** * @Title: PoiTlTest * @Description: * @author: leon * @date: 2023/3/14 19:11 */public class PoiTlTest {    public static void main(String[] args) {        try {            Map map = new HashMap();            /*嵌套*/            List subData = new ArrayList();            subData.add(new AddrModel("Guangdong,China"));            subData.add(new AddrModel("Shanghai,China"));            map.put("nested", Includes.ofLocal("D:\subInTest.docx").setRenderModel(subData).create());            XWPFTemplate.compile("D:\testin.docx").render(map).writeToFile("D:\5.docx");        } catch (IOException e) {            e.printStackTrace();        }    }}class AddrModel {    private String addr;    public AddrModel(String addr) {        this.addr = addr;    }    // Getter/Setter}

             效果:

            6、条件判断显示

            标签 :

            {{?condition}}   需要显示的内容    {{/condition}} 

            模板:

            代码示例:

               public static void main(String[] args) {        try {            Map map = new HashMap();            /*条件判断显示*/            map.put("condition1", false); //不显示,默认值也为false            map.put("condition1Str","显示1");            map.put("condition2", true);            map.put("condition2Str","显示2");            XWPFTemplate.compile("D:\testin.docx").render(map).writeToFile("D:\6.docx");        } catch (IOException e) {            e.printStackTrace();        }    }

             效果:

             7、非空集合循环

            标签也为 :

            {{?songs}}   需要遍历的内容  {{/songs}} 

            模板:

            代码示例:

                public static void main(String[] args) {        try {            Map map = new HashMap();            /*非空集合循环*/            Map map1 = new HashMap();            map1.put("star","周杰伦");            map1.put("song","---兰亭序");            Map map2 = new HashMap();            map2.put("star","林俊杰");            map2.put("song","---江南");            map.put("songs", Arrays.asList(map1,map2));            XWPFTemplate.compile("D:\testin.docx").render(map).writeToFile("D:\7.docx");        } catch (IOException e) {            e.printStackTrace();        }    }

             效果:

            8、图表

            图标有单系列图表、多系列图表、组合图表多种类型,下面只介绍单系列图表的使用:

            标签:{{var}}

            注意:该标签不是直接在模板定义,而是在:图表区格式—可选文字—标题(新版本Microsoft Office标签位置在:编辑替换文字-替换文字)。

            下面是Microsoft Office的操作步骤:

            点击插入,选择相应的图表

             

            编辑替换文字-替换文字

            代码示例:

            public static void main(String[] args) { try {            Map map = new HashMap();            /*图表*/            //单系列图表指的是饼图(3D饼图)、圆环图等。            map.put("chart1",Charts                    .ofSingleSeries("综合测评结果统计", new String[] { "正常", "异常","其他" })                    .series("badAndgood", new Integer[] { 19, 17,15})                    .create());            XWPFTemplate.compile("D:\testin.docx").render(map).writeToFile("D:\8.docx");        } catch (IOException e) {            e.printStackTrace();        }    }

             效果:

            总结

             上面是poi-tl的一些简单用例,如对其他功能感兴趣的朋友,可以去poi-tl的官网学习,地址为:http://deepoove.com/poi-tl/

上一篇:腾讯小直播源码怎么样?

下一篇:Java常见的PDF报表生成方式,报表导出,源码