先引入maven依赖:
<!--easypoi依赖-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
<!--阿里巴巴easyExcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.7</version>
</dependency>
<!-- 文件上传依赖 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
工具类:
package com.sport.sporttrailwalkadminserver.common.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.List;
import java.util.Map;
import java.util.NoSuchElementException;
public class FileUtil {
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);
}
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
if (workbook != null);
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) {
e.printStackTrace();
//throw new NotFoundException(e.printStackTrace());
}
}
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
if (workbook != null);
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){
e.printStackTrace();
//throw new NotFoundException("模板不能为空");
} catch (Exception e) {
e.printStackTrace();
//throw new NotFoundException(e.getMessage());
}
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 new NotFoundException("excel文件不能为空");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
// throw new NotFoundException(e.getMessage());
}
return list;
}
}
list<Map> 转List<对象>工具
package com.sport.sporttrailwalkadminserver.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sport.sporttrailwalkadminserver.po.trailwalk.TrailActivityApplyExport;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.util.StringUtils;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class ListUtil {
/**
* targetList 需要处理的list
* targetClass 目标class
* @return List 处理好的list
*/
public List listMapParseListObj(List targetList, Class targetClass){
// 先获取该类的代理对象
Object obj = null;
try {
obj = targetClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// 将list转为list对象
for(int i = 0; i< targetList.size(); i++){
Map map = (Map)targetList.get(i); // 获取每个list里面每个map
Iterator it = map.keySet().iterator();
while(it.hasNext()){
String name = it.next().toString(); // 名称 key
String value = map.get(name).toString(); // 值 value
try{
//取得值的类形
Class type = PropertyUtils.getPropertyType(obj, name);
if(!StringUtils.isEmpty(type)){
PropertyUtils.setProperty(obj, name, ConvertUtils.convert(value, type));
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
// 将list<Map> 转为对象进行返回
List resListObj = Lists.newArrayList();
for (Object object : targetList) {
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(object));
resListObj.add(jsonObject.toJavaObject((Type)targetClass));
}
//返回封装好的集合
return resListObj;
}
}
sql对应的实体类:
package com.sport.sporttrailwalkadminserver.po.trailwalk;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ExcelTarget("TrailActivityApplyExport")
public class TrailActivityApplyExport implements Serializable {
@Excel(name = "姓名", orderNum = "1", width = 25,needMerge = true)
private String name;
@Excel(name = "性别", orderNum = "2", width = 25,needMerge = true)
private String sex;
@Excel(name = "出生年月日", orderNum = "3", width = 25,needMerge = true)
private String birthday; //出生年月日
@Excel(name = "证件类型", orderNum = "4", width = 25,needMerge = true)
private String license_type; //证件类型(1身份证号, 2护照, 3港澳台, 4其他)
@Excel(name = "证件号码", orderNum = "5", width = 25,needMerge = true)
private String license_number;
@Excel(name = "联系方式", orderNum = "6", width = 25,needMerge = true)
private String mobile;
@Excel(name = "报名时间", orderNum = "7", width = 25,needMerge = true)
private String apply_time;
@Excel(name = "状态", orderNum = "8", width = 25,needMerge = true)
private String apply_status; //报名状态(1正常,2禁赛)
}
获取sql集合:
导出:
导入: