easypoi必填项_EasyExcel对Excel⽂件的解析过程
POI与easyExcel的区别:
、
POI是通过WorkBook来解析表格⽂件的,虽然内部有使⽤到Sax模式,能后⼤⼤的提⾼效率,但是要解析⼤⽂件(10w,100w⾏)的话很
容易出现OOM(内存溢出)。
相⽐之下,
1、easyExcel解析实在磁盘上进⾏的,⼏乎可以将⼏mb级别的内存压缩到kb级别,⼏乎不⽤担⼼OOM;
2、⽤Java模型进⾏关系映射,项⽬中最常⽤的就Java模型映射,通过 @ExcelProperty注解就可以完成⾏与列的映射;
3、easyExcel中有⼀个类AnalysisEventListener,⾥⾯有⼀个⽅法invoke实现了⼀⾏⼀⾏返回,另外还可以重写该类的doAfterAllAnalysed⽅法,⽤来做事后处理之类的操作,相当的灵活。
准备阶段
第⼀步:引⼊easyExcel依赖
com.alibaba
easyexcel
1.1.2-beta5
View Code
第⼆步:⾃定义ExcelListener继承于AnalysisEventListener,重写invoke()⽅法(可以读取到excel每⼀⾏的数据)和doAfterAllAnalysed()⽅法(⽤于后置处理),其中datas⽤于存取读取到的数据,importHeads为导⼊表头,modelHeads
packagecom.cloud.data.utils;l.context.AnalysisContext;l.event.AnalysisEventListe EasyExcell导⼊监听类*/
public class ExcelListener extendsAnalysisEventListener {//⾃定义⽤于暂时存储数据
private List datas = new ArrayList<>();//导⼊表头
private String importHeads = "";//模版表头
private String modelHeads = "";/*** 通过 AnalysisContext对象获取当前sheet,当前⾏等数据*/@Overridepublic
voidinvoke(Object o, AnalysisContext analysisContext) {
Integer CurrentRowNum();//获取导⼊表头,默认第⼀⾏为表头
if(currentRowNum == 0){try{
Map m =objToMap(o);for(Object v : m.values()) {if(!StringUtils.isEmpty(v)){
importHeads+= String.valueOf(v).trim() + ",";
}
}
}catch(Exception e) {
e.printStackTrace();
}
}else{
datas.add(o);
}
}/*** 获取模板表头*/@Overridepublic voiddoAfterAllAnalysed(AnalysisContext analysisContext) {//获取模版表头
ExcelHeadProperty ehp =ExcelHeadProperty();for(Lists : Head()){
modelHeads+= s.get(0) + ",";
}
}//Object转换为Map
private Map objToMap(Object obj) throwsException{
Map map = new LinkedHashMap();
Field[] Class().getDeclaredFields();for(Field field : fields){
field.setAccessible(true);
map.Name(), (obj));
}returnmap;
}public ListgetDatas() {returndatas;
}public void setDatas(Listdatas) {this.datas =datas;
}publicString getImportHeads() {returnimportHeads;
}public voidsetImportHeads(String importHeads) {this.importHeads =importHeads;
}publicString getModelHeads() {returnmodelHeads;
}public voidsetModelHeads(String modelHeads) {delHeads =modelHeads;
}
}
View Code
第三步:添加接收excel导⼊的实体类OutDbillDto,继承BaseRowModel,其中@ExcelProperty(value = "机组调度名称(必填)", index
= 0)对应excel表头的(value为表头名称,index为索引位置)
packagecom.ity.dto;l.annotation.ExcelProperty;l.metadata.BaseRow ⽇结算账单导⼊类*/@Datapublic class OutDbillDto extends BaseRowModel implementsSerializable {
@ExcelProperty(value= "机组调度名称(必填)", index = 0)privateString unitDispatchName;
@ExcelProperty(value= "⽇期(必填)", index = 1)privateString billTime;
@ExcelProperty(value= "收费项⽬", index = 2)privateString project;
@ExcelProperty(value= "本期电量(万kWh)", index = 3)privateString quan;
@ExcelProperty(value= "单价(厘/kWh)", index = 4)privateString avgPrice;
@ExcelProperty(value= "本期电费(元)",index = 5)privateString price;/**标记*/
privateString mark;/**错误信息*/
privateString errMsg;
}
View Code
第四步,定义EasyExcelUtil⼯具类,⽤于读取excel
packagecom.cloud.data.utils;l.EasyExcelFactory;l.ExcelReader;importcom.alibaba java.io.*;importjava.nio.file.Path;importjava.nio.file.Paths;import java.util.*;public classEasyExcelUtil {/*** 读取某个 sheet 的
param nameExcel
*
*@paramexcel ⽂件
*@paramrowModel 实体类映射,继承 BaseRowModel 类
*@returnExcel 数据 list*/
public static List readExcel(MultipartFile excel, BaseRowModel rowModel) throwsIOException {return readExcel(excel, rowModel, 1, 1);
}/*** 读取某个 sheet 的 Excel
*@paramexcel ⽂件
*@paramrowModel 实体类映射,继承 BaseRowModel 类
*@paramsheetNo sheet 的序号 从1开始
*@returnExcel 数据 list*/
public static Map readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo) throwsIOException {
Map result = new HashMap<>();
ExcelListener excelListener= newExcelListener();
ExcelReader reader=getReader(excel, excelListener);if(Objects.isNull(reader)) {return null;
}
Boolean flag = false;ImportHeads().ModelHeads())){
flag= true;
}
result.put("flag", flag);
result.put("datas", Datas());returnresult;
}/*** 读取某个 sheet 的 Excel
*@paramexcel ⽂件
*@paramrowModel 实体类映射,继承 BaseRowModel 类
*@paramsheetNo sheet 的序号 从1开始
*@paramheadLineNum 表头⾏数,默认为1
*@returnExcel 数据 list*/
public static List readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo, int headLineNum) throwsIOException {
ExcelListener excelListener= newExcelListener();
ExcelReader reader=getReader(excel, excelListener);if (reader == null) {return null;
}
}/*** 读取指定sheetName的Excel(多个 sheet)
*@paramexcel ⽂件
*@paramrowModel 实体类映射,继承 BaseRowModel 类
*@returnExcel 数据 list
*@throwsIOException*/
public static List readExcel(MultipartFile excel, BaseRowModel rowModel,String sheetName) throwsIOException {
ExcelListener excelListener= newExcelListener();
ExcelReader reader=getReader(excel, excelListener);if (reader == null) {return null;
}for(Sheet sheet : Sheets()) {if (rowModel != null) {
sheet.Class());
}//读取指定名称的sheet
SheetName().contains(sheetName)){
}
}Datas();
}/*** 返回 ExcelReader
*@paramexcel 需要解析的 Excel ⽂件
*@paramexcelListener new ExcelListener()
*@throwsIOException*/
private static ExcelReader getReader(MultipartFile excel,ExcelListener excelListener) throwsIOException {
String OriginalFilename();Null(filename) && (LowerCase().endsWith(".xls") || LowerCase().endsWith(".xlsx"))){
InputStream is= InputStream());return new ExcelReader(is, null,null, excelListener, false);
}else{return null;
}
}/*** 将导⼊失败的数据写到系统指定路径
*@paramfailDatas 数据
*@paramname ⽂件名
*@paramdir 写⼊的路径
*@paramheader Excel表头
*@paramcolumns 需要写⼊Excel的对象属性集合
*@paramindex 合并单元格索引
*@return*@throwsException*/
public static String saveExcel2Loacl(List> failDatas, String name, String dir, String[] header,String[] columns,Integer index) throwsException {//1.唯⼀⽂件名
String fileName = name + "_" + NowStr(DateUtil.TIME_FORMAT)+".xls";
Path (ExcelUtil.FILE_UPLOAD_ROOT, dir).toAbsolutePath();
File file= String());if (!ists()){
file.mkdirs();
}//2.添加Sheet名
Sheet sheet = new Sheet(1,0);
sheet.setSheetName(name);//3.动态添加Excel表头
List> head = new ArrayList<>();for(String h : header) {
head.wArrayList(h));
}
sheet.setHead(head);//4.写⼊数据
List datas = newArrayList(failDatas);
List> data = new ArrayList<>();for(Object var : datas) {
List objects =wArrayList();
Arrays.stream(columns).forEach(e->{try{
String Property(var, e);
objects.add(property);
}catch(Exception err) {
err.printStackTrace();
}
});
data.add(objects);
}
FileOutputStream fileOutputStream= (ExcelUtil.FILE_UPLOAD_ROOT, dir, fileName).toFile());
ExcelWriter writer= Writer(fileOutputStream, ExcelTypeEnum.XLS, true);
writer.write1(data,sheet);//5.合并单元格
for (int i = 1; i <= failDatas.size(); i = i +index) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论