參考文章:https://blog.csdn.net/lchq1995/article/details/84823592
https://blog.csdn.net/hzygcs/article/details/85100608
https://blog.csdn.net/w903328615/article/details/88426235
官方文檔:https://opensource.afterturn.cn/doc/easypoi.html#4
參考語法:
空格分割
三目運算 {{test ? obj:obj2}}
n: 表示 這個cell是數值類型 {{n:}}
le: 代表長度{{le:()}} 在if/else 運用{{le:() > 8 ? obj1 : obj2}}
fd: 格式化時間 {{fd:(obj;yyyy-MM-dd)}}
fn: 格式化數字 {{fn:(obj;###.00)}}
fe: 遍歷數據,創建row
!fe: 遍歷數據不創建row
$fe: 下移插入,把當前行,下面的行全部下移.size()行,然後插入
#fe: 橫向遍歷
v_fe: 橫向遍歷值
!if: 刪除當前列 {{!if:(test)}}
單引號表示常量值 '' 比如'1' 那麼輸出的就是 1
&NULL& 空格
]] 換行符 多行遍歷導出
sum: 統計數據
實現步驟:導入Maven依賴:
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.1.0</version>
</dependency>
代碼實現:
package com.example.demo;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
/**
* @author zycstart
* @create 2020-04-11 12:47
*/
@RestController
public class TestExcelExport {
@RequestMapping
public void fe_map(HttpServletResponse response) throws Exception {
TemplateExportParams params = new TemplateExportParams(
"D:\\專項支出用款申請書_map.xls", new Integer[]{0, 1, 2});
Map<String, Object> map = new HashMap<>();
map.put("date", "2014-12-25");
map.put("money", 2000000.00);
map.put("upperMoney", "貳佰萬");
map.put("company", "執筆潛行科技有限公司");
map.put("bureau", "財政局");
map.put("person", "JueYue");
map.put("phone", "1879740****");
List<Map<String, String>> listMap = new ArrayList<>();
for (int i = 0; i < 4; i++) {
Map<String, String> lm = new HashMap<>();
lm.put("id", i + 1 + "");
lm.put("zijin", i * 10000 + "");
lm.put("bianma", "A001");
lm.put("mingcheng", "設計");
lm.put("xiangmumingcheng", "EasyPoi " + i + "期");
lm.put("quancheng", "開源項目");
lm.put("sqje", i * 10000 + "");
lm.put("hdje", i * 10000 + "");
listMap.add(lm);
}
map.put("maplist", listMap);
Map<Integer, Map<String, Object>> map1 = new HashMap();
map1.put(0, map);
map1.put(1, map);
map1.put(2, map);
Workbook workbook = ExcelExportUtil.exportExcel(map1, params);
File savefile = new File("D:/excel/");
//刪除文件夾下所有內容
if (!savefile.exists()) {
savefile.mkdirs();
}
FileOutputStream fos = new FileOutputStream("D:/excel/專項支出用款申請書導出.xls" );
workbook.write(fos);
fos.close();
export(response,workbook,"專項支出用款申請書導出.xls");
}
// Excel 導出 通過瀏覽器下載的形式
public static void export(HttpServletResponse response, Workbook workbook, String fileName) throws IOException {
response.setHeader("Content-Disposition",
"attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso8859-1"));
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(response.getOutputStream());
workbook.write(bufferedOutPut);
bufferedOutPut.flush();
bufferedOutPut.close();
}
}