Beetl模版引擎
0.簡介
??本文只簡單介紹使用hutool中的BeetlUtil結(jié)合FileWriter實(shí)現(xiàn)模版文件的數(shù)據(jù)渲染
??前提:pom引入hutool工具類
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.10</version>
</dependency>
1.編寫模版文件
示例:
1.1 模版文件
文件路徑:E:\model\easyModel.txt
#日期:${billDate}
#-----------------------------------------成績列表----------------------------------------
姓名,年齡,成績
<%
for(student in list){
%>
${student.name},'${student.id},${student.age},${student.score}
<% } %>
#-----------------------------------------成績列表----------------------------------------
#導(dǎo)出時(shí)間:${time,dateFormat="yyyy-MM-dd HH:mm:ss"}
Excel顯示數(shù)字時(shí),如果數(shù)字大于12位,它會(huì)自動(dòng)轉(zhuǎn)化為科學(xué)計(jì)數(shù)法重归;如果數(shù)字大于15位,它不僅用于科學(xué)技術(shù)法表示威始,還會(huì)只保留高15位,其他位都變0像街。所以需要在id這里加一個(gè)符合【'】
1.2 實(shí)體類:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Student implements Serializable {
private static final long serialVersionUID = -7271996307387758147L;
private String name;
private String id;
private Integer age;
private Integer score;
}
1.3 測試類
public static void main(String[] args) {
String billDate = "2020-04-29";
Map<String,Object> map = Maps.newHashMap();
map.put("time",new Date());
map.put("billDate",billDate);
Student student1 = Student.builder().name("小李").age(18).score(90)
.id("1005004171908261748056081").build();
Student student2 = Student.builder().name("小白").age(19).score(99)
.id("1005008601908261125575301").build();
List<Student> list = Lists.newArrayList();
list.add(student1);
list.add(student2);
map.put("list",list);
String content = BeetlUtil.render("E:\\model\\","easyModel.txt",map);
// 3.創(chuàng)建文件
FileWriter writer = new FileWriter("E:\\model\\output.csv", CharsetUtil.CHARSET_GBK);
writer.write(content);
}
1.4 實(shí)際效果
6.png