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

相关文章

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

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

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

Python变量与数据类型全解析(最新整理)

《Python变量与数据类型全解析(最新整理)》文章介绍Python变量作为数据载体,命名需遵循字母数字下划线规则,不可数字开头,大小写敏感,避免关键字,本文给大家介绍Python变量与数据类型全解析... 目录1、变量变量命名规范python数据类型1、基本数据类型数值类型(Number):布尔类型(bo

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

MySQL复杂SQL之多表联查/子查询详细介绍(最新整理)

《MySQL复杂SQL之多表联查/子查询详细介绍(最新整理)》掌握多表联查(INNERJOIN,LEFTJOIN,RIGHTJOIN,FULLJOIN)和子查询(标量、列、行、表子查询、相关/非相关、... 目录第一部分:多表联查 (JOIN Operations)1. 连接的类型 (JOIN Types)