EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel。
github地址:https://github.com/alibaba/easyexcel
语雀地址:https://www.yuque.com/easyexcel/doc/easyexcel
@Data
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 5)
@HeadFontStyle(fontHeightInPoints = 15, color = 2)
@HeadRowHeight(20)
@ContentRowHeight(15)
@ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER)
@ColumnWidth(35)
@ApiModel(value = "DemoDto")
public class DemoDto{
@ExcelProperty(value = "名称", index = 0)
private String name;
@ExcelProperty(value = "日期", index = 1)
private Date date;
@ExcelProperty(value = "数量", index = 2)
private Integer count;
@ApiModelProperty(value = "行号")
@ExcelIgnore
private Integer rowIndex;
}
/**
* 第一个Sheet写入的数据
*/
private List<DemoDto> getDemoData1() {
List<DemoDto> dtoList = new ArrayList<DemoDto>();
List<DemoVo> voList = this.demoMapper.getDemoData1();
for (DemoVo vo : voList) {
DemoDto dto = new DemoDto();
dto.setName(vo.getName());
dto.setDate(vo.getDate());
dto.setCount(vo.getCount());
dtoList.add(dto);
}
return dtoList;
}
/**
* 第二个Sheet写入的数据
*/
private DemoDto getDemoData2() {
DemoVo vo = this.demoMapper.getDemoData2();
DemoDto dto = new DemoDto();
dto.setName(vo.getName());
dto.setDate(vo.getDate());
dto.setCount(vo.getCount());
return dto;
}
表头样式:HeadStyleHandler
自动列宽:LongestMatchColumnWidthStyleStrategy
@GetMapping("download")
public void download(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("Web写样例", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
WriteSheet writeSheet1 = EasyExcel.writerSheet(0, "第一个Sheet得名称").head(DemoDto.class)
.registerWriteHandler(new HeadStyleHandler())
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.build();
excelWriter.write(getDemoData1(), writeSheet1);
WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "第二个Sheet得名称").head(DemoDto.class)
.registerWriteHandler(new HeadStyleHandler())
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.build();
excelWriter.write(Arrays.asList(getDemoData2()), writeSheet2);
//关闭流
excelWriter.finish();
}
@Data
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 5)
@HeadFontStyle(fontHeightInPoints = 15, color = 2)
@HeadRowHeight(20)
@ContentRowHeight(15)
@ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER)
@ColumnWidth(35)
@ApiModel(value = "DemoDto")
public class DemoDto{
@ExcelProperty(value = "名称", index = 0)
private String name;
@ExcelProperty(value = "日期", index = 1)
private Date date;
@ExcelProperty(value = "数量", index = 2)
private Integer count;
@ApiModelProperty(value = "行号")
@ExcelIgnore
private Integer rowIndex;
}
@Slf4j
public class DemoListener extends AnalysisEventListener<DemoDto> {
// 最大处理条数
private static final int BATCH_COUNT = 5;
List<DemoDto> list = new ArrayList<>();
private DemoService demoService;
public DemoListener () {
}
/**
* 构造函数,传入处理类
*
* @param demoService
*/
public DemoListener (DemoService demoService) {
this.demoService= demoService;
}
/**
* 解析每一条数据调用方法
*
* @param data
* @param context
*/
@Override
public void invoke(DemoDtodata, AnalysisContext context) {
log.info("解析数据:{}", JSON.toJSONString(data));
data.setRowIndex(context.readRowHolder().getRowIndex() + 1);
list.add(data);
if (list.size() >= BATCH_COUNT) {
list.clear();
}
}
/**
* 解析完成调用方法
*
* @param context
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
log.info("所有数据解析完成!");
}
}
public interface DemoService {
/**
* 导入数据
*
* @param file
* @return
*/
List<DemoDto> importData(MultipartFile file);
}
@Slf4j
@Service
public class DemoServiceImpl implements DemoService {
@Autowired
private DemoService demoService ;
/**
* 导入数据
*
* @param file
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public List<DemoDto> importData(MultipartFile file) {
// 获取excel数据
List<DemoDto> dtoList = null;
try {
InputStream input1Stream = file.getInputStream();
List<DemoDto> dto1List = EasyExcel.read(input1Stream , new DemoListener (this))
.head(DemoDto.class).sheet(0, "第一个Sheet得名称").headRowNumber(3).doReadSync();
InputStream input2Stream = file.getInputStream();
List<DemoDto> dto2List = EasyExcel.read(input2Stream , new DemoListener (this))
.head(DemoDto.class).sheet(1, "第二个Sheet得名称").headRowNumber(3).doReadSync();
dtoList.addAll(dto1List);
dtoList.addAll(dto2List);
} catch (IOException e) {
e.printStackTrace();
}
return dtoList;
}
}