1.导入依赖
<!-- excel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
导出
首先创建要导出的对象。
/**
* @Author: lv
* @CreateDate: 2022/11/15 11:27 上午
* @Description:
*/
@Data
public class PayorderExcelDto {
@ExcelProperty("支付订单号")
private String payOrderId;
/**
* 商户号
*/
@ExcelProperty("商户号")
private String mchNo;
}
@ExcelProperty(“列名称”) 这个代表了字段和excel的列名称的映射关系。
然后接口部分代码是这样的:
List<PayOrder> payOrders = payOrderService.listBy(payOrder, paramJSON, wrapper);
List<PayorderExcelDto> excelDtos = new ArrayList<>();
payOrders.forEach(e->{
PayorderExcelDto payorderExcelDto = new PayorderExcelDto();
BeanUtil.copyProperties(e,payorderExcelDto);
payorderExcelDto.setLiquidateState(PayorderExcelDto.getLiquidateStateEnumValue(e.getLiquidateState()));
payorderExcelDto.setState(PayorderExcelDto.getStateEnumValue(e.getState()));
payorderExcelDto.setNotifyState(PayorderExcelDto.getNotifyStateEnumValue(e.getNotifyState()));
excelDtos.add(payorderExcelDto);
});
String filename = "payOrder.xlsx";
response.setHeader("Content-disposition", "attachment;filename=" + filename);
try {
//最重要的就是这句,将集合写入resp数据流中
EasyExcel.write(response.getOutputStream(), PayorderExcelDto.class).sheet("订单数据").doWrite(excelDtos);
} catch (IOException e) {
throw new RuntimeException("导出失败!" + e.getMessage());
}
xls 版本的Excel最多一次可写0 …65535行
xlsx 版本的Excel最多一次可写0…1048575行
导入
创建监听器。 继承 AnalysisEventListener 这个类。
例子:
package com.atguigu.srb.core.listener;
@Slf4j
//@AllArgsConstructor //全参
@NoArgsConstructor //无参
public class ExcelDictDTOListener extends AnalysisEventListener<ExcelDictDTO> {
/**
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 5;
List<ExcelDictDTO> list = new ArrayList();
private DictMapper dictMapper;
//传入mapper对象
public ExcelDictDTOListener(DictMapper dictMapper) {
this.dictMapper = dictMapper;
}
/**
*遍历每一行的记录
* @param data
* @param context
*/
@Override
public void invoke(ExcelDictDTO data, AnalysisContext context) {
log.info("解析到一条记录: {}", data);
list.add(data);
// 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
if (list.size() >= BATCH_COUNT) {
saveData();
// 存储完成清理 list
list.clear();
}
}
/**
* 所有数据解析完成了 都会来调用
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 这里也要保存数据,确保最后遗留的数据也存储到数据库
saveData();
log.info("所有数据解析完成!");
}
/**
* 加上存储数据库
*/
private void saveData() {
log.info("{}条数据,开始存储数据库!", list.size());
dictMapper.insertBatch(list); //批量插入
log.info("存储数据库成功!");
}
}
接口:
@PostMapping("/import")
public R batchImport(
@ApiParam(value = "Excel文件", required = true)
@RequestParam("file") MultipartFile file) {
try {
InputStream inputStream = file.getInputStream();
EasyExcel.read(inputStream, ExcelDictDTO.class, new ExcelDictDTOListener(dictMapper)).sheet().doRead();
return R.ok().message("批量导入成功");
} catch (Exception e) {
//UPLOAD_ERROR(-103, "文件上传错误"),
throw new BusinessException(ResponseEnum.UPLOAD_ERROR, e);
}
}