代码是这样的
package com.cwf.wms.utility;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.cwf.wms.product.entity.Product;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import sun.misc.Unsafe;
import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@Component
@Profile("dev")
@Log4j2
public class LoadExcelToDataBase {
@PostConstruct
public <T> void read() {
disableWarning();
String file = "C:\\Users\\Administrator\\Desktop\\workdoc\\h2\\H2_PUBLIC_PRODUCT.xlsx";
EasyExcel.read(file, Product.class, new AnalysisEventListener() {
@Override
public void invoke(Object o, AnalysisContext analysisContext) {
log.trace("read from sheet {}, row {}:{}", analysisContext.readSheetHolder().getSheetName(), analysisContext.readRowHolder().getRowIndex(), o);
System.out.println("数据加载成功!!!!");
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("数据加载成功22222!!!!");
}
}).sheet("0").registerConverter(new ExcelToDataConvert()).doRead();
}
public static void disableWarning() {
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe u = (Unsafe) theUnsafe.get(null);
Class cls = Class.forName("jdk.internal.module.IllegalAccessLogger");
Field logger = cls.getDeclaredField("logger");
u.putObjectVolatile(cls, u.staticFieldOffset(logger), null);
} catch (Exception e) {
// ignore
}
}
}