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

相关文章

CSS去除a标签的下划线的几种方法

《CSS去除a标签的下划线的几种方法》本文给大家分享在CSS中,去除a标签(超链接)的下划线的几种方法,本文给大家介绍的非常详细,感兴趣的朋友一起看看吧... 在 css 中,去除a标签(超链接)的下划线主要有以下几种方法:使用text-decoration属性通用选择器设置:使用a标签选择器,将tex

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

Mysql中深分页的五种常用方法整理

《Mysql中深分页的五种常用方法整理》在数据量非常大的情况下,深分页查询则变得很常见,这篇文章为大家整理了5个常用的方法,文中的示例代码讲解详细,大家可以根据自己的需求进行选择... 目录方案一:延迟关联 (Deferred Join)方案二:有序唯一键分页 (Cursor-based Paginatio

Java利用poi实现word表格转excel

《Java利用poi实现word表格转excel》这篇文章主要为大家详细介绍了Java如何利用poi实现word表格转excel,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、每行对象类需要针对不同的表格进行对应的创建。package org.example.wordToEx

国内环境搭建私有知识问答库踩坑记录(ollama+deepseek+ragflow)

《国内环境搭建私有知识问答库踩坑记录(ollama+deepseek+ragflow)》本文给大家利用deepseek模型搭建私有知识问答库的详细步骤和遇到的问题及解决办法,感兴趣的朋友一起看看吧... 目录1. 第1步大家在安装完ollama后,需要到系统环境变量中添加两个变量2. 第3步 “在cmd中

Maven pom.xml文件中build,plugin标签的使用小结

《Mavenpom.xml文件中build,plugin标签的使用小结》本文主要介绍了Mavenpom.xml文件中build,plugin标签的使用小结,文中通过示例代码介绍的非常详细,对大家的学... 目录<build> 标签Plugins插件<build> 标签<build> 标签是 pom.XML

Mysql中InnoDB与MyISAM索引差异详解(最新整理)

《Mysql中InnoDB与MyISAM索引差异详解(最新整理)》InnoDB和MyISAM在索引实现和特性上有差异,包括聚集索引、非聚集索引、事务支持、并发控制、覆盖索引、主键约束、外键支持和物理存... 目录1. 索引类型与数据存储方式InnoDBMyISAM2. 事务与并发控制InnoDBMyISAM

StarRocks索引详解(最新整理)

《StarRocks索引详解(最新整理)》StarRocks支持多种索引类型,包括主键索引、前缀索引、Bitmap索引和Bloomfilter索引,这些索引类型适用于不同场景,如唯一性约束、减少索引空... 目录1. 主键索引(Primary Key Index)2. 前缀索引(Prefix Index /