本文相當(dāng)于筆記,主要根據(jù)官方文檔Poi-tl Documentation和poi-tl的使用(最全詳解)_JavaSupeMan的博客-CSDN博客文章進(jìn)行學(xué)習(xí)(上班夠用)
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@EqualsAndHashCode
public class Author {
private String name;
private String sex;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@EqualsAndHashCode
public class Student implements Serializable {
private String name;
private String startTime;
private Author author;
private String img;
}
兩個(gè)實(shí)體類
下邊測(cè)試類
@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class Demo {
@Test
public void Tx01() throws IOException {
Map<String, Object> datas = new HashMap<String, Object>();
// 對(duì)象形式 存儲(chǔ)對(duì)象
Student student = new Student("華", "2023-09-02 20:41:10",
new Author("張三", "男"),null);
datas.put("student",student);
// 處理文字樣式(下劃線等)
// Style underlineStyle = Style.builder().underline().build();
datas.put("var",Texts.of("hello").bold().italic().create());
// 圖片 http://deepoove.com/images/icecream.png
// datas.put("image","http://deepoove.com/images/icecream.png");
// datas.put("svg","http://deepoove.com/images/icecream.png");
// datas.put("svg",Pictures.ofUrl("http://deepoove.com/images/icecream.png")
// .size(100,100).create());
datas.put("image", Pictures.ofUrl("https://ee19hua.oss-cn-beijing.aliyuncs.com/images/2022/09/24/1664008700443.jpg")
.size(400, 300).create());
// 表格 基本操作
//RowRenderData就是指定每一行數(shù)據(jù)的,可以去官網(wǎng)查閱,這里相當(dāng)于設(shè)置了三行,row0就是表頭,row1,row2是表內(nèi)容
RowRenderData row0 = Rows.of("姓名", "學(xué)歷").textColor("FFFFFF")
.bgColor("4472C4").center().create();
RowRenderData row1 = Rows.create("李四", "博士");
RowRenderData row2 = Rows.create("李四", "博士");
MergeCellRule rule1 = MergeCellRule.builder()
.map(MergeCellRule.Grid.of(0,0),MergeCellRule.Grid.of(0,1)).build();//合并單元格(第幾行第幾列)
datas.put("var1", Tables.of(row0, row1,row2).mergeRule(rule1).create());
//合并單元格
RowRenderData roW0 = Rows.of("列0", "列1", "列2", "列3")
.bgColor("4472C4")
.textColor("7F7f7F").textFontFamily("Hei").textFontSize(15).center().create();
RowRenderData roW1 = Rows.create("沒(méi)有數(shù)據(jù)", null, null, null);//第一行
//合并第幾行第幾列 到 第幾行第幾列
MergeCellRule rule = MergeCellRule.builder()
.map(MergeCellRule.Grid.of(1, 0), MergeCellRule.Grid.of(1, 2)).build();
datas.put("var2", Tables.of(roW0, roW1).mergeRule(rule).create());
// 動(dòng)態(tài)生成表格 列表循環(huán) Configure.builder()
Student s1 = new Student("張三", "2023-09-02 20:41:10",
new Author("趙", "男"),"https://ee19hua.oss-cn-beijing.aliyuncs.com/images/2022/09/24/1664008700443.jpg");
Student s2 = new Student("李四", "2023-09-02 20:41:10",
new Author("錢", "男"),"https://ee19hua.oss-cn-beijing.aliyuncs.com/images/2022/09/24/1664008700443.jpg");
Student s3 = new Student("王五", "2023-09-02 20:41:10",
new Author("孫", "女"),"https://ee19hua.oss-cn-beijing.aliyuncs.com/images/2022/09/24/1664016579528.png");
Student s4 = new Student("趙六", "2023-09-02 20:41:10",
new Author("李", "女"),"https://ee19hua.oss-cn-beijing.aliyuncs.com/images/2022/09/24/1664008700443.jpg");
List<Student> studentList = new ArrayList<>();
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
datas.put("lists",studentList);
// 插件列表,可以去官網(wǎng)查看,有列循環(huán),還有行循環(huán),這里是行循環(huán)實(shí)例
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
//這里可以指定一個(gè) config 類,用來(lái)指定一些規(guī)則,也可以改變模板中{{}}的這種格式
Configure config = Configure.builder()
.bind("lists", policy).build();
// 列表
datas.put("listString",studentList);
//模板地址
String templateFilePath = "d:/data/template/";
//生成文件的保存地址
String destFilePath = "d:/data/template/word";
// 生成word
XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "test1.docx",config);
// XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "test3.docx",config);
compile.render(datas);
//輸出為文件,指定輸出文件名
compile.writeToFile(destFilePath+"out_test01.docx");
System.out.println("運(yùn)行完成-------------------------------------");
}
}
word模板
?生成結(jié)果:
?
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-691472.html
如果想生成柱狀圖等,按照官網(wǎng)進(jìn)行學(xué)習(xí)即可,以上兩篇(官網(wǎng),和博客學(xué)習(xí)夠用)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-691472.html
到了這里,關(guān)于POI-TL制作word的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!