本文基于文章【动态导出Word文档,poi-tl的前世今生及快速使用】,对文本、表格、图片、列表、图表等做详细的示例记录。
快速开始
- 引入依赖
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.2</version>
</dependency>
- 文本输出数据
/**
* 填充数据源- 文本数据
*/
public static Map<String, Object> textData(Map<String, Object> data) {
//key要与模板中的别名一一对应
data.put("custNo", "嘎嘎嘎");
data.put("custCnName", "样式文本");
data.put("productCategory", Texts.of("超链接").link("http://www.baidu.com.cn").create());
data.put("orgKey", "4433");
data.put("attachRealName", Texts.of("锚点文本").anchor("custNo").create());
data.put("riskLevel", "1001");
return data;
}
- 表格数据
/**
* 填充数据源- 表格数据
*/
public static Map<String, Object> tableData(Map<String, Object> map) {
List<Map<String,Object>> list = new ArrayList<>();
//1 表格1
map.put("table0", Tables.of(new String[][] {
new String[] { "表头1", "表头1" ,"表头三"},
new String[] { "数据1", "数据2","数据3" }
}).border(BorderStyle.DEFAULT).create());
//2 表头居中且设置背景色的表格
RowRenderData row0 = Rows.of("姓名", "学历","年龄").textColor("FFFFFF")
.bgColor("4472C4").center().create();
RowRenderData row1 = Rows.create("张三", "博士","18");
RowRenderData row2 = Rows.create("李四", "本科","20");
map.put("table1", Tables.create(row0, row1,row2));
//3 合并第1行所有单元格的表格
RowRenderData row00 = Rows.of("111", "222", "333").center().bgColor("4472C4").create();
RowRenderData row11 = Rows.create("没有数据", null, null);
//规定合并单元格规则
MergeCellRule rule = MergeCellRule.builder().map(MergeCellRule.Grid.of(1, 0), MergeCellRule.Grid.of(1, 2)).build();
map.put("table2", Tables.of(row00, row11).mergeRule(rule).create());
return map;
}
- 列表数据
/**
* 填充数据源- 列表数据
*/
public static Map<String, Object> listData(Map<String, Object> map) {
ArrayList<String> strings = new ArrayList<>();
strings.add("大白菜");
strings.add("胡萝卜");
strings.add("牛肉");
map.put("list1",strings);
/**
* 指定列表样式
* DECIMAL //1. 2. 3.
* DECIMAL_PARENTHESES //1) 2) 3)
* BULLET //● ● ●
* LOWER_LETTER //a. b. c.
* LOWER_ROMAN //i ⅱ ⅲ
* UPPER_LETTER //A. B. C.
*/
map.put("list2", Numberings.of(NumberingFormat.DECIMAL).addItem("香蕉").addItem("苹果").addItem("橘子").create());
return map;
}
- 图表数据
/**
* 填充数据源-图表
*/
public static Map<String, Object> chartData(Map<String, Object> map) throws Exception{
//柱形图
ChartMultiSeriesRenderData chart = Charts
.ofMultiSeries("全国医院综合排名", new String[] { "友好医院","泰山医院","协和医院","第二人民医院","第三医院"})
.addSeries("数据排名", new Double[] { 60.5,50.6,85.4,29.7,35.4})
.addSeries("价格排名", new Double[] { 50.5,65.6,75.4,82.7,95.4})
.create();
map.put("barChart", chart);
//折线图
ChartMultiSeriesRenderData qst = Charts
.ofMultiSeries("成长趋势", new String[] { "07-10","07-11","07-12","07-13","07-14","07-15"})
.addSeries("公众号", new Double[] { 60.5,40.6,22.7,85.4,500.0,40.8})
.addSeries("抖音", new Double[] { 40.5,50.6,62.7,45.4,160.0,440.8})
.addSeries("小红书", new Double[] { 170.5,520.6,362.7,305.4,200.0,140.8})
.create();
map.put("zxtChart", qst);
//饼图
ChartSingleSeriesRenderData pie = Charts
.ofSingleSeries("各省份GDP对比", new String[] { "河南", "四川","北京","上海" })
.series("经济占比", new Integer[] { 50, 35,60,55 })
.create();
map.put("pieChart", pie);
//组合图表
ChartMultiSeriesRenderData comb = Charts
.ofComboSeries("技术语言成长", new String[] { "java", "Python" })
.addBarSeries("2021", new Double[] { 15.0, 6.0 })
.addBarSeries("2022", new Double[] { 223.0, 119.0 })
.addLineSeries("2023", new Double[] { 323.0, 89.0 })
.addLineSeries("2024", new Double[] { 123.0, 59.0 }).create();
map.put("zhChart", comb);
//柱状图、折线图组合
ChartMultiSeriesRenderData hntb = Charts
.ofComboSeries("河南城市排名", new String[] { "南阳","郑州","洛阳","信阳","驻马店","安阳"})
.addBarSeries("GDP",new Double[] {50.5,40.6,32.7,85.4,500.0,49.8})
.addBarSeries("人口",new Double[] {60.5,50.6,82.7,45.4,300.0,120.8})
.addLineSeries("指数",new Double[] {10.5,50.3,30.7,80.4,20.3,40.8})
.create();
map.put("gcChart", hntb);
return map;
}
- 文档生成示例
@GetMapping("/getWordFile")
public void getWordFile(){
try {
String filePath = "G:\\产品详情.docx";//模板路径
Map<String,Object> dataMap = getData();
log.info("响应信息:{}",dataMap);
String custCnName = dataMap.get("custCnName")+"";
InputStream inputStream = new FileInputStream(filePath);
String fileName = custCnName + "-产品风险查询详情";
String url = "G:\\"+fileName + ".docx";
//解析模板
XWPFTemplate template = XWPFTemplate.compile(filePath);
//渲染数据
template.render(dataMap);
//以文件形式输出
template.writeAndClose(new FileOutputStream(url));
} catch (Exception e) {
log.error("导出异常:{}",e);
throw new RuntimeException(e);
}
}
public Map<String, Object> getData() throws Exception {
Map<String, Object> map = new HashMap<>();
map = textData(map);
map = tableData(map);
map = tableData1(map);
map = imgData(map);
map = listData(map);
map = chartData(map);
return map;
}
- 准备模板
文本:直接使用{{属性}}
列表:以*开头,使用{{*list}}
图片:以@开头,使用{{@img}}
表格:以#开头,使用{{#table}}
图表:- 在模板中插入需要使用的图表
- 在图表空白处右键 -> 选择设置图表区域格式 -> 文本选项 -> 可选文字-> 在标题中设置属性{{pieChart}}
- 在模板中插入需要使用的图表
- 文档生成示例
文本:
图片&表格:
列表:
图表:
a. 饼图&柱状图
b. 折线图:
c. 组合图:
d. 折线&柱状图组合:
更多使用,参见官方文档。
仓库地址:
https://gitcode.com/gh_mirrors/po/poi-tl/overview
文档地址:
https://deepoove.com/poi-tl/#_why_poi_tl
参考:文章 FC464782123