EasyPoi
使用模板导出数据
1.引入依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
2.工具类
package com.junfeng.utils;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelUtil {
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException {
ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException {
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF));
}
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
defaultExport(list, fileName, response);
}
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (StringUtils.isBlank(filePath)) {
return Collections.emptyList();
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
}
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception {
if (file == null) {
return Collections.emptyList();
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
return ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
}
public static <T> List<T> importExcel(MultipartFile file, int sheetIndex, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception {
if (file == null) {
return Collections.emptyList();
}
ImportParams params = new ImportParams();
params.setStartSheetIndex(sheetIndex);
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
return ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
}
public static <T> List<T> importExcel(String filePath, int sheetIndex, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
ImportParams params = new ImportParams();
params.setStartSheetIndex(sheetIndex);
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
}
public static Map<String, Object> createOneSheet(String sheetName, Class<?> clazz, List<?> data) {
ExportParams params = new ExportParams("", sheetName, ExcelType.XSSF);
return createOneSheet(params, clazz, data);
}
private static Map<String, Object> createOneSheet(ExportParams params, Class<?> clazz, List<?> data) {
Map<String, Object> map = new HashMap<>(8);
map.put("title", params);
map.put("entity", clazz);
map.put("data", data);
return map;
}
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
if (workbook != null) {
downLoadExcel(fileName, response, workbook);
}
}
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.XSSF);
if (workbook != null) {
downLoadExcel(fileName, response, workbook);
}
}
public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
}
}
3.导出代码
@Override
public void export(List<StorageCapacityVO> result, HttpServletResponse response) throws IOException {
TemplateExportParams params = new TemplateExportParams(
"doc/example.xlsx");
Map<String, Object> map = new HashMap<>();
List<Map<String, String>> listMap = new ArrayList<>();
String yyyymm = "";
String dd = "";
for (int i = 0; i < result.size(); i++) {
Map<String, String> lm = new HashMap<>();
StorageCapacityVO vo = result.get(i);
Date tm = vo.getTm();
dd = DateUtil.format(tm, "dd");
String time = DateUtil.format(tm, "HH:mm:ss");
yyyymm = DateUtil.format(tm, "yyyy 年 MM 月");
lm.put("time", time == null ? "" : time);
lm.put("water", vo.getWater() == null ? "" : vo.getWater() + "");
lm.put("power", vo.getPower() == null ? "" : vo.getPower() + "");
lm.put("ecologyFlow", vo.getEcologyFlow() == null ? "" : vo.getEcologyFlow() + "");
lm.put("downFlow", vo.getDownFlow() == null ? "" : vo.getDownFlow() + "");
lm.put("warehousing", vo.getWarehousing() == null ? "" : vo.getWarehousing() + "");
lm.put("q", vo.getQ() == null ? "" : vo.getQ() + "");
lm.put("z", vo.getZ() == null ? "" : vo.getZ() + "");
listMap.add(lm);
}
map.put("maplist", listMap);
map.put("yyyymm", yyyymm);
map.put("dd", dd);
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
String fileName = "file_" + dd + ".xlsx";
downLoadExcel(fileName, response, workbook);
}