需要引入的jar包
//excel导出
implementation group: 'cn.afterturn', name: 'easypoi-base', version: '4.2.0'
implementation group: 'cn.afterturn', name: 'easypoi-web', version: '4.2.0'
implementation group: 'cn.afterturn', name: 'easypoi-annotation', version: '4.2.0'
自定义工具类
FileWithExcelUtil
package com.sgmart.utils.excel;
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidationConstraint;
import org.apache.poi.xssf.usermodel.XSSFDataValidationHelper;
import org.apache.poi.xssf.usermodel.XSSFSheet;
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.List;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* excel工具类.
*/
@Slf4j
public class FileWithExcelUtil {
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) {
ExportParams exportParams = new ExportParams(title, sheetName);
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) {
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
defaultExport(list, fileName, response);
}
public static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
downLoadExcel(fileName, response, workbook);
}
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
log.error("[monitor][IO][表单功能]", e);
}
}
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
downLoadExcel(fileName, response, workbook);
}
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
} catch (NoSuchElementException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return list;
}
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (file == null) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
} catch (NoSuchElementException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
log.error("[monitor][表单功能]", e);
}
return list;
}
/**
* firstRow 開始行號 根据此项目,默认为2(下标0开始)
* lastRow 根据此项目,默认为最大65535
* firstCol 区域中第一个单元格的列号 (下标0开始)
* lastCol 区域中最后一个单元格的列号
* strings 下拉内容
*/
public static void selectList(Workbook workbook, int firstCol, int lastCol, String[] strings) {
Sheet sheet = workbook.getSheetAt(0);
// 生成下拉列表
// 只对(x,x)单元格有效
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(2, 65535, firstCol, lastCol);
// 生成下拉框内容
DataValidation dataValidation;
if (sheet instanceof HSSFSheet) {
DataValidationConstraint dvConstraint = DVConstraint.createExplicitListConstraint(strings);
dataValidation = new HSSFDataValidation(cellRangeAddressList, dvConstraint);
} else {
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFSheet) sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
.createExplicitListConstraint(strings);
dataValidation = dvHelper.createValidation(dvConstraint, cellRangeAddressList);
}
// 对sheet页生效
sheet.addValidationData(dataValidation);
}
}
最终实现demo
package com.sgmart.utils.excel.demo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.sgmart.utils.excel.FileWithExcelUtil;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
/**
* 描述:导入导出模板.
*
* @author ppliu
* created in 2020/12/18 9:13
*/
@Data
class Template {
@Excel(name = "序号", width = 10)
private Long index;
@Excel(name = "名称", format = "yyyy-MM-dd HH:mm:ss", width = 40)
private String name;
@Excel(name = "时间", format = "yyyy-MM-dd HH:mm:ss", width = 40)
private LocalDateTime time;
@Excel(name = "图片", type = 2, width = 20, imageType = 2)
private byte[] image;
/**
* 导入demo
*/
private List<Template> importTest(MultipartFile file) {
return FileWithExcelUtil.importExcel(file, 0, 1, Template.class);
}
/**
* 导出demo
*/
private void exportDemo(HttpServletResponse response,List<Template> templateList) {
FileWithExcelUtil.exportExcel(templateList, "权益数据", "权益数据", Template.class, "权益数据.xls", true, response);
}
}