1.依賴
如果項(xiàng)目有原始poi 依賴 可以使用以下兼容版本
<properties>
<poi.version>3.15</poi.version>
<easypoi.version>3.3.0</easypoi.version>
</properties>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>${easypoi.version}</version>
</dependency>
如果項(xiàng)目沒(méi)有引入poi 依賴 可以直接用下面依賴
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
2.導(dǎo)出方法?
public static void excel() {
Map total =new HashMap<>();
List> mapList =new ArrayList<>();
for (int i =1; i <=5; i++) {
Map map =new HashMap<>();
map.put("id", i +"");
/* map.put("bin", "001 1000千克");
? ? ? ? map.put("name", "商品" + i);
map.put("code", "goods" + i);
map.put("proDate", "2019-05-30");
map.put("recvDate", "2019-07-07");*/
? ? ? ? mapList.add(map);
}
total.put("list", mapList);
total.put("code","code9527");
TemplateExportParams params =new TemplateExportParams("export/" +"test4.xlsx",true);
try {
Workbook workbook = ExcelExportUtil.exportExcel(params, total);
// CellRangeAddress cra = new CellRangeAddress(1, 3, 0, 0);
// workbook.getSheetAt(0).addMergedRegion(cra);
? ? ? ? FileOutputStream fileOutputStream =new FileOutputStream("F:\\project\\工時(shí)系統(tǒng)資料" + File.separator +"1.xls");
workbook.write(fileOutputStream);
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
3.模板?
位置在resources 創(chuàng)建export文件夾里面放要導(dǎo)出的模板
簡(jiǎn)單模板(單行遍歷)
復(fù)雜模板多行遍歷(有單元格合并情況)
效果圖
復(fù)雜模板2
效果圖
注意點(diǎn) 模板指令可以參考官方文檔
空格分割
三目運(yùn)算 {{test ? obj:obj2}}
n: 表示 這個(gè)cell是數(shù)值類型 {{n:}}
le: 代表長(zhǎng)度{{le:()}} 在if/else 運(yùn)用{{le:() > 8 ? obj1 : obj2}}
fd: 格式化時(shí)間 {{fd:(obj;yyyy-MM-dd)}}
fn: 格式化數(shù)字 {{fn:(obj;###.00)}}
fe: 遍歷數(shù)據(jù),創(chuàng)建row
!fe: 遍歷數(shù)據(jù)不創(chuàng)建row
$fe: 下移插入,把當(dāng)前行,下面的行全部下移.size()行,然后插入
#fe: 橫向遍歷
v_fe: 橫向遍歷值
!if: 刪除當(dāng)前列 {{!if:(test)}}
單引號(hào)表示常量值 '' 比如'1' 那么輸出的就是 1
&NULL& 空格
]] 換行符 多行遍歷導(dǎo)出
sum: 統(tǒng)計(jì)數(shù)據(jù)
如果在表格表頭下面空一行在填寫循環(huán)的指令 導(dǎo)出的數(shù)據(jù)也會(huì)空一行在填充數(shù)據(jù)
如果需要做統(tǒng)計(jì)的信息 如果循環(huán)次數(shù)可以確定 可以在下面合適行添加統(tǒng)計(jì)的信息如果循環(huán)次數(shù)不確定可以在表格表頭下面做統(tǒng)計(jì)信息展示?
獲取導(dǎo)出數(shù)據(jù)中code 值 可以在模板合適單元格位置直接 {{code}} 即可