POI-tl 知识整理:整理2 -> 标签

2024-01-14 05:28
文章标签 整理 知识 标签 tl poi

本文主要是介绍POI-tl 知识整理:整理2 -> 标签,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 文本标签

{{var}}

数据模型:

  • String :文本

  • TextRenderData :有样式的文本

  • HyperlinkTextRenderData :超链接和锚点文本

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

代码示例:

    @Testpublic void testTextLabel() throws Exception{Student student = new Student();student.setName("小蟹");student.setAge(20);student.setSex("男");XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates2.docx");Map<String, Object> map = new HashMap<>();map.put("name",new TextRenderData("Eff000", student.getName()));map.put("link", new HyperlinkTextRenderData("链接", "http://www.baidu.com") );map.put("anchor", new HyperlinkTextRenderData("回到最顶端", "anchor: appendix1"));XWPFTemplate render = template.render(map);FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_object.docx");template.writeAndClose(fileOutputStream);template.close();  // 一定要记得关闭}

 链式代码示例:

    @Testpublic void testTextLabel() throws Exception{Student student = new Student();student.setName("小蟹");student.setAge(20);student.setSex("男");XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates2.docx");Map<String, Object> map = new HashMap<>();/** 可以使用链式写法:* */// 可以通过这种方式设置样式,在下方可以直接get对应的样式//Style style = new Style();//style.setStrike(true);//style.setUnderlinePatterns(UnderlinePatterns.SINGLE);//style.setVertAlign(String.valueOf(VerticalAlign.SUPERSCRIPT));map.put("name", Texts.of(student.getName()).color("FF0000").bold().fontSize(20).fontFamily("楷体").italic().create());map.put("link", Texts.of("链接").color("FF0000").link("http://bilibili.com").create());map.put("anchor", Texts.of("回到最顶端").color("8E6000").italic().anchor("anchor: appendix1").create());XWPFTemplate render = template.render(map);FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_object.docx");template.writeAndClose(fileOutputStream);template.close();  // 一定要记得关闭}

运行结果:

2 图片标签

图片标签以@开始:{{@var}}

数据模型:

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

  • PictureRenderData

  • ByteArrayPictureRenderData

  • FilePictureRenderData

  • UrlPictureRenderData

推荐使用工厂 Pictures 构建图片模型。

示例代码:

    @Testpublic void testImgLabel() throws Exception{XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates_imgs.docx");Map<String, Object> map = new HashMap<>();// 1. 指定本地路径图片map.put("img", "D:\\Idea-projects\\POI_word\\girl1.jpg");// 2. 指定在线图片map.put("girlOnline", "https://c-ssl.duitang.com/uploads/blog/202108/21/20210821132505_66c30.jpg");// 3.指定本地路径图片, 并设置大小map.put("imgSize", Pictures.ofLocal("D:\\Idea-projects\\POI_word\\girl3.jpg").size(100, 100).create());// 4. 图片流map.put("StreamImg", Pictures.ofStream(new FileInputStream("D:\\Idea-projects\\POI_word\\girl1.jpg"), PictureType.JPEG).size(300, 250).create());// 5. 网络图片(注意网络耗时对系统可能的性能影响)map.put("urlImg", Pictures.ofUrl("https://c-ssl.duitang.com/uploads/blog/202108/21/20210821132505_66c30.jpg").size(300, 250).create());// 6. java图片BufferedImage bufferImage = new BufferedImage(300, 250, BufferedImage.TYPE_INT_RGB);// 首先需要填充 bufferImage(这一部分根据自身需要展示的图,填充bufferImage)// 获取 Graphics2D 对象Graphics2D g2d = bufferImage.createGraphics();// 绘制红色背景g2d.setColor(Color.RED);g2d.fillRect(0, 0, bufferImage.getWidth(), bufferImage.getHeight());// 绘制黑色文本g2d.setColor(Color.BLACK);g2d.setFont(new Font("Arial", Font.BOLD, 20));((Graphics2D) g2d).drawString("Hello World!", 50, 120);// 释放 Graphics2D 对象资源g2d.dispose();map.put("buffered_image", Pictures.ofBufferedImage(bufferImage, PictureType.JPEG).size(300, 250).create());XWPFTemplate render = template.render(map);FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_img.docx");template.writeAndClose(fileOutputStream);template.close();  // 一定要记得关闭}

3 表格标签

表格标签以#开始:{{#var}}

数据模型: 

·TableRenderData

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

 

3.1 基础表格示例

    @Testpublic void testTableLabel() throws Exception{XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates_table.docx");Map<String, Object> map = new HashMap<>();// 推荐使用工厂 Tables 、 Rows 和 Cells 构建表格模型。// 1. 基础表格示例TableRenderData tableRenderData = Tables.of(new String[][]{new String[]{"00", "01"},new String[]{"10", "11"},}).border(BorderStyle.DEFAULT).create();map.put("table0", tableRenderData);XWPFTemplate render = template.render(map);FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_table.docx");template.writeAndClose(fileOutputStream);template.close();  // 一定要记得关闭}

3.2 表格样式示例

    @Testpublic void testTableLabel() throws Exception{XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates_table.docx");Map<String, Object> map = new HashMap<>();// 推荐使用工厂 Tables 、 Rows 和 Cells 构建表格模型。// 2. 表格样式示例RowRenderData row0 = Rows.of("姓名", "学历").textColor("FFFFFF").textBold().bgColor("4472C4").center().rowExactHeight(3.0).create();RowRenderData row1 = Rows.create("张三", "本科");RowRenderData row2 = Rows.create("李四", "硕士");TableRenderData tableRenderData1 = Tables.create(row0, row1, row2);map.put("table1", tableRenderData1);XWPFTemplate render = template.render(map);FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_table.docx");template.writeAndClose(fileOutputStream);template.close();  // 一定要记得关闭}

3.3 表格合并示例

    @Testpublic void testTableLabel() throws Exception{XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates_table.docx");Map<String, Object> map = new HashMap<>();// 推荐使用工厂 Tables 、 Rows 和 Cells 构建表格模型。// 可以通过这种方式设置单元格样式CellStyle cellStyle = new CellStyle();cellStyle.setBackgroundColor("006400");// 3. 表格合并示例RowRenderData row3 = Rows.of("列0", "列1", "列2").center().bgColor(cellStyle.getBackgroundColor()).create();RowRenderData row4 = Rows.create("没有数据", null, null);//来指定合并规则//这里的 (1, 0) 表示第一行第一列的单元格,(1, 2) 表示第一行第三列的单元格MergeCellRule rule = MergeCellRule.builder().map(MergeCellRule.Grid.of(1, 0), MergeCellRule.Grid.of(1, 2)).build();//将合并规则应用到表格中TableRenderData tableRenderData2 = Tables.of(row3, row4).mergeRule(rule).create();map.put("table2", tableRenderData2);XWPFTemplate render = template.render(map);FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_table.docx");template.writeAndClose(fileOutputStream);template.close();  // 一定要记得关闭}

4 列表标签

列表标签以*开始:{{*var}}

数据模型:

  • List<String>

  • NumberingRenderData

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

代码示例:

    @Testpublic void testListLabel() throws Exception{XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates_list.docx");Map<String, Object> map = new HashMap<>();//推荐使用工厂 Numberings 构建列表模型NumberingRenderData numberingRenderData = Numberings.of(LOWER_ROMAN)  // 可以有多种有序、无序编号方式.addItem("列表1").addItem("列表2").addItem("列表2").create();map.put("list", numberingRenderData);XWPFTemplate render = template.render(map);FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_list.docx");template.writeAndClose(fileOutputStream);template.close();  // 一定要记得关闭}

运行结果:

5 区块对标签

区块对由前后两个标签组成,开始标签以?标识,结束标签以/标识:{{?sections}}{{/sections}}

5.1 False 或 空集合

如果区块对的值是 null 、false 或者空的集合,位于区块中的所有文档元素将不会显示,这就等同于if语句的条件为 false。

5.2 非False 且不是集合

如果区块对的值不为 null 、 false ,且不是集合,位于区块中的所有文档元素会被渲染一次,这就等同于if语句的条件为 true。

    @Testpublic void testSectionLabel() throws Exception{XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates_section.docx");Map<String, Object> map = new HashMap<>();HashMap<String, HashMap<String, String>> data = new HashMap<>();HashMap<String, String> dataMin = new HashMap<>();dataMin.put("name", "xiexu");data.put("person", dataMin);map.put("person",data.get("person"));XWPFTemplate render = template.render(map);FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_section.docx");template.writeAndClose(fileOutputStream);template.close();  // 一定要记得关闭}

 

5.3 非空集合

如果区块对的值是一个非空集合,区块中的文档元素会被迭代渲染一次或者N次,这取决于集合的大小,类似于foreach语法。

6 嵌套标签

嵌套又称为导入、包含或者合并,以+标识:{{+var}}

数据模型:

·DocxRenderData

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

代码示例:

public class AddrModel {public String addr;public AddrModel(String addr) {this.addr = addr;}public String getAddr() {return addr;}public void setAddr(String addr) {this.addr = addr;}
}

    @Testpublic void testQiantaoLabel() throws Exception{XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates_Qiantao.docx");Map<String, Object> map = new HashMap<>();ArrayList<AddrModel> list = new ArrayList<>();list.add(new AddrModel("Beijing,China"));list.add(new AddrModel("Shanghai,China"));map.put("nested", Includes.ofLocal("D:\\Idea-projects\\POI_word\\sub.docx").setRenderModel(list).create());XWPFTemplate render = template.render(map);FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_Qiantao.docx");template.writeAndClose(fileOutputStream);template.close();  // 一定要记得关闭}

运行结果:

这篇关于POI-tl 知识整理:整理2 -> 标签的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/604064

相关文章

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

MyBatis的xml中字符串类型判空与非字符串类型判空处理方式(最新整理)

《MyBatis的xml中字符串类型判空与非字符串类型判空处理方式(最新整理)》本文给大家介绍MyBatis的xml中字符串类型判空与非字符串类型判空处理方式,本文给大家介绍的非常详细,对大家的学习或... 目录完整 Hutool 写法版本对比优化为什么status变成Long?为什么 price 没事?怎

Python按照24个实用大方向精选的上千种工具库汇总整理

《Python按照24个实用大方向精选的上千种工具库汇总整理》本文整理了Python生态中近千个库,涵盖数据处理、图像处理、网络开发、Web框架、人工智能、科学计算、GUI工具、测试框架、环境管理等多... 目录1、数据处理文本处理特殊文本处理html/XML 解析文件处理配置文件处理文档相关日志管理日期和

Python38个游戏开发库整理汇总

《Python38个游戏开发库整理汇总》文章介绍了多种Python游戏开发库,涵盖2D/3D游戏开发、多人游戏框架及视觉小说引擎,适合不同需求的开发者入门,强调跨平台支持与易用性,并鼓励读者交流反馈以... 目录PyGameCocos2dPySoyPyOgrepygletPanda3DBlenderFife

Python自动化批量重命名与整理文件系统

《Python自动化批量重命名与整理文件系统》这篇文章主要为大家详细介绍了如何使用Python实现一个强大的文件批量重命名与整理工具,帮助开发者自动化这一繁琐过程,有需要的小伙伴可以了解下... 目录简介环境准备项目功能概述代码详细解析1. 导入必要的库2. 配置参数设置3. 创建日志系统4. 安全文件名处

MySQL 迁移至 Doris 最佳实践方案(最新整理)

《MySQL迁移至Doris最佳实践方案(最新整理)》本文将深入剖析三种经过实践验证的MySQL迁移至Doris的最佳方案,涵盖全量迁移、增量同步、混合迁移以及基于CDC(ChangeData... 目录一、China编程JDBC Catalog 联邦查询方案(适合跨库实时查询)1. 方案概述2. 环境要求3.

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注