关于EasyExcel的上传和导出
1.导出。easyExcel与传统的poi相比,速度更快,数据量大也能快速导出。因为底层封装的方法是一行一行导出,比所有数据一起导出会快。
@Autowired
FestivalService festivalService;
@RequestMapping(value = "/exportData.ctrl")
public void exportData(HttpServletResponse response,@RequestBody Map<String, Object> param) throws IOException {
log.getLogger("test_c").info("testexport({},{})", "开始",param);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("年节数据导出", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
List<NewYearData> sublist = new ArrayList<NewYearData>();
sublist=festivalService.selectDataExport(param);
EasyExcel.write(response.getOutputStream(), NewYearData.class).sheet("模板").doWrite(sublist);
log.getLogger("test_c").info("exportEnd({})", "结束");
}
创建一个实体类
public class NewYearBackend {
@ExcelProperty("用户手机号")
private String mobileNumber;
@ExcelProperty("分享时间")
private String shareAt;
@ExcelProperty("拉新俱乐部会员数")
private int newShare ;
@ExcelProperty("拉新游戏用户数")
private int gameShare;
@ExcelProperty("获得碎片总数")
private int frageCount ;
@ExcelProperty("分享总次数")
private int shareTimes ;
@ExcelProperty("会员编号")
private int accountId ;
@ExcelProperty("是否完成问卷")
private String ifDone;
@ExcelProperty("游戏获得碎片数")
private int gameCount ;
@ExcelProperty("分享获得碎片数")
private int shareCount ;
@ExcelProperty("官方链接碎片数")
private int officeCount ;
@ExcelProperty("问卷获得碎片数")
private int questionCount;
@ExcelProperty("剩余碎片数")
private int saveCount ;}
2.导入 第一步创建一个实体类,第二步,创建一个Listener implements ReadListener<T> 第三步,调用通用方法。
public class DemoListener<T> implements ReadListener<T> {
public static final int BATCH_COUNT = 3000;
/**
* Temporary storage of data
*/
private List<T> cachedData = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
/**
* consumer
*/
private final Consumer<List<T>> consumer;
public DemoListener(Consumer<List<T>> consumer) {
this.consumer = consumer;
}
@Override
public void invoke(T data, AnalysisContext context) {
cachedData.add(data);
if (cachedData.size() >= BATCH_COUNT) {
consumer.accept(cachedData);
// 存储完成清理 list
cachedData = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
consumer.accept(cachedData);
}
}
public class DemoData {
@ExcelProperty("名字")
private String string;
@DateTimeFormat("yyyy-MM-dd")
@ExcelProperty("日期")
private String date;
@ExcelProperty("数字")
private Double doubleData;
@RequestMapping(value = "/import.ctrl", method = RequestMethod.POST, produces = "application/json")
public String importExcel( @RequestParam(required = false) MultipartFile file) throws IOException {
EasyExcel.read(file.getInputStream(), DemoData.class, new DemoListener<DemoData>(dataList -> {
for (DemoData demoData : dataList) {
log.getLogger("test_c").info("excelData:({})", demoData);
}
})).sheet().doRead();
log.getLogger("test_c").info("importExcelEnd({})", "结束");
return "success";
}