工具类
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
/**
* 高性能处理Excel工具类
*
*/
public class EasyExcelUtil {
/**
* 使用 模型 来读取Excel
*
* @param inputStream Excel的输入流
* @param clazz 模型的类
* @return 返回 模型 的列表
*/
public static <T> List<T> readExcel(InputStream inputStream, Class<T> clazz) {
ModelExcelListener<T> listener = new ModelExcelListener<T>();
EasyExcel.read(inputStream, clazz, listener).sheet().doRead();
return listener.getDatas();
}
/**
* 使用 模型 来导出到WEB
*
* @param response web的响应
* @param data 要写入的以 模型 为单位的数据
* @param fileName 配置Excel的表名
* @param sheetName 配置Excel的页签名
* @param clazz 模型的类
* @throws IOException
*/
public static <T> void writeExcel(HttpServletResponse response, List<T> data, Class<T> clazz, String fileName, String sheetName) throws IOException {
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data);
}
/**
* 使用 模型 来写入Excel
* <br/>注意,如果是web输出流,需要设置头
*
* @param outputStream Excel的输出流
* @param data 要写入的以 模型 为单位的数据
* @param sheetName 配置Excel的表名字
* @param clazz 模型的类
*/
public static <T> void writeExcel(OutputStream outputStream, List<T> data, Class<T> clazz, String sheetName) {
EasyExcel.write(outputStream, clazz).sheet(sheetName).doWrite(data);
}
/**
* 模型 解析监听器
*/
private static class ModelExcelListener<T> extends AnalysisEventListener<T> {
private List<T> datas = new ArrayList<>();
@Override
public void invoke(T object, AnalysisContext context) {
datas.add(object);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
public List<T> getDatas() {
return datas;
}
}
}
模板下载、导出实体定义(实体可以公用)
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
@ExcelIgnoreUnannotated //默认情况下Java类中的所有属性都添加读写,在类上面加入@ExcelIgnoreUnannotated注解,加入这个注解后只有加了@ExcelProperty才会参与读写。
public class 实体类名 {
@TableId(value = "主键字段", type = IdType.INPUT)
private String xxx;
@ExcelProperty(value = "表格模板名字")
@TableField(value = "数据库属性对应字段")
private String xxxx;
}
模板下载Controller(接口直接调用工具类即可)
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author
*/
@RestController
@RequestMapping("/")
@Api(value = "", tags = "")
public class Controller {
/**
* 下载模板
*/
@PostMapping(value = "/")
@ApiOperation(value = "下载导入模板", notes = "下载导入模板")
public ResultData downLoadOwnerVehicleForm(HttpServletResponse response) {
List<实体> xxxxx= new ArrayList<>();
try {
EasyExcelUtil.writeExcel(response, ownerVehicleForms, 实体.class, "模板名字", "sheet1");
} catch (IOException e) {
e.printStackTrace();
}
return new ResultData<>(ResultData.SUCCESS);
}
}
表格导入Controller
@PostMapping(value = "/")
@ApiOperation(value = "", notes = "")
public ResultData xxxxx(@RequestParam MultipartFile file) {
byte[] byteArr;
try {
byteArr = file.getBytes();
} catch (IOException e) {
throw new xxxException("500", "文件格式转换异常");
}
InputStream inputStream = new ByteArrayInputStream(byteArr);
List<实体> xxxxxx= null;
xxxxxx = EasyExcelUtil.readExcel(inputStream, 实体.class);
if (CollectionUtils.isEmpty(xxxxxx)) {
throw new xxxException("500", "导入数据不可为空");
}
//此处拿到导入数据 进入业务层处理
return new ResultData<>(ResultData.SUCCESS);
}
。。。。业务层省略 根据实际需求处理
可以添加事务回滚
@Transactional(rollbackFor = xxxException.class)
不知道有没有遗漏的 自用就这样 随机应变吧