数据大小30M左右
耗费时间8秒
package io.renren.modules.tollstation.utils;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import io.renren.modules.tollstation.entity.TollStationRevenueEntity;
import io.renren.modules.tollstation.entity.TollStationTrafficEntity;
import io.swagger.models.auth.In;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author 李亚杰
* @date 2024/4/24 15:49
* @description LiyajieExcelUtil1
*/
public class LiyajieExcelUtil2 {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\liyajie\\IdeaProjects\\monthScore\\renren-admin\\src\\main\\resources\\2023年通行费收入统计表-税前(公司、路段)20240116.xlsx");
// File file = new File("C:\\Users\\liyajie\\IdeaProjects\\monthScore\\renren-admin\\src\\main\\resources\\202301-流量——收费站出入口 (恢复的).xlsx");
// File file = new File("C:\\Users\\liyajie\\IdeaProjects\\monthScore\\renren-admin\\src\\main\\resources\\学生.xlsx");
long l1 = System.currentTimeMillis();
// List<TollStationTrafficEntity> dispose = dispose(file, 1, 4, 4, 20000, TollStationTrafficEntity.class);
// System.out.println(dispose.size());
List<Integer> index= new ArrayList<>();
index.add(0);
index.add(1);
readExcel(file, TollStationRevenueEntity.class, 1,4, 4);
// readExcel(file);
long l2 = System.currentTimeMillis();
System.out.println("耗费时间"+(l2-l1));
}
/**
* 读取Excel文件并将合并单元格的数据解析
* @param file excel文件
* @param tClass 类Class对象
* @param sheetIndex sheet索引0代表第一张表
* @param startIndex 起始位置,一般为表头行数
* @param endIndex 结束位置
* @param index Excel的表格需要处理的合并单元格的列数,从0开始,0代表处理第一列的合并单元格数据,0,1代表处理第1,2列的合并单元格数据,以此类推
*/
public static <T> List<T> readExcel(File file,Class<T> tClass,int sheetIndex,int startIndex,int endIndex,List<Integer> index) {
List<T> userDataList = new ArrayList<>();
EasyExcel.read(file, tClass, new AnalysisEventListener<T>() {
@Override
public void invoke(T userData, AnalysisContext analysisContext) {
userDataList.add(userData);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// 所有数据解析完成后可以执行的操作,例如关闭流等
}
}).headRowNumber(startIndex).sheet(sheetIndex).doRead();
List<T> tList = userDataList.subList(0, userDataList.size() - endIndex);
final T[] t = (T[]) new Object[]{tList.get(0)};
tList.forEach(o->{
try {
copyNonNullValues(t[0],o,index);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
t[0] =o;
});
return tList;
}
/**
* 读取Excel文件
* @param file excel文件
* @param tClass 类Class对象
* @param sheetIndex sheet索引0代表第一张表
* @param startIndex 起始位置,一般为表头行数
* @param endIndex 结束位置
*/
public static <T> List<T> readExcel(File file,Class<T> tClass,int sheetIndex,int startIndex,int endIndex) {
return readExcel(file,tClass,sheetIndex,startIndex,endIndex,null);
}
/**
* 读取Excel文件
* @param file excel文件
* @param tClass 类Class对象
* @param sheetIndex sheet索引0代表第一张表
* @param startIndex 起始位置,一般为表头行数
*/
public static <T> List<T> readExcel(File file,Class<T> tClass,int sheetIndex,int startIndex) {
return readExcel(file,tClass,sheetIndex,startIndex,0,null);
}
/**
* 读取Excel文件,默认读取第一张表
* @param file excel文件
* @param tClass 类Class对象
* @param startIndex 起始位置,一般为表头行数
*/
public static <T> List<T> readExcel(File file, Class<T> tClass, int startIndex) {
return readExcel(file,tClass,0,startIndex,0,null);
}
/**
* 读取Excel文件,默认读取第一张表,默认表头只有一行
* @param file excel文件
* @param tClass 类Class对象
*/
public static <T> List<T> readExcel(File file,Class<T> tClass) {
return readExcel(file,tClass,0,1,0,null);
}
/**
* 读取Excel文件,默认读取第一张表,默认表头只有一行
* @param file excel文件
*/
public static List<Object> readExcel(File file) {
return EasyExcel.read(file).sheet().doReadSync();
}
/**
* 将s对象中的不为空的值赋值给t对象中为空的值
* @param source s对象
* @param target t对象
* @param index 需要赋值的属性的列数
* @param <T>
* @throws IllegalAccessException
*/
public static <T> void copyNonNullValues(T source, T target,List<Integer> index) throws IllegalAccessException {
if (index==null||index.size()==0) return;
Class<?> sourceClass = source.getClass();
Class<?> targetClass = target.getClass();
// 确保source和target是同一类型或其子类
if (!sourceClass.isAssignableFrom(targetClass) && !targetClass.isAssignableFrom(sourceClass)) {
throw new IllegalArgumentException("Source and target objects must be of compatible types");
}
// 获取source对象的所有字段
Field[] sourceFields = sourceClass.getDeclaredFields();
//获取需要处理的列数
for (int i : index) {
Field sourceField = sourceFields[i];
sourceField.setAccessible(true); // 允许访问私有字段
// 获取target对象中对应的字段
try {
Field targetField = targetClass.getDeclaredField(sourceField.getName());
targetField.setAccessible(true); // 允许访问私有字段
// 获取source字段的值
Object sourceValue = sourceField.get(source);
// 如果source字段的值非空,且target字段的值为空,则复制值
if (sourceValue != null && (targetField.get(target) == null || "".equals(targetField.get(target)))) {
targetField.set(target, sourceValue);
}
} catch (NoSuchFieldException e) {
// 忽略target对象中不存在的字段
}
}
}
}