触发场景描述 引入依赖,按照demo示例读文件和写文件
触发Bug的代码
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.4</version>
</dependency>
@Data
public class MyLogEntity {
@ExcelProperty("dt")
private String dt;
@ExcelProperty("hour")
private String hour;
@ExcelProperty("ctime")
private String ctime;
@ExcelProperty("ip")
private String ip;
@ExcelProperty("status")
private String status;
public String getDt() {
return dt;
}
public void setDt(String dt) {
this.dt = dt;
}
public String getHour() {
return hour;
}
public void setHour(String hour) {
this.hour = hour;
}
public String getCtime() {
return ctime;
}
public void setCtime(String ctime) {
this.ctime = ctime;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
public class MyLogEntityListener extends AnalysisEventListener<MyLogEntity> {
private static final Logger LOGGER = LoggerFactory.getLogger(MyLogEntityListener.class);
private static final Gson GSON = new Gson();
@Override
public void invoke(MyLogEntity myLogEntity, AnalysisContext analysisContext) {
LOGGER.info("解析到一条数据:{}", GSON.toJson(myLogEntity));
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
LOGGER.info("所有数据解析完成!");
}
}
public class ExcelTest {
@Test
public void read() {
String fileName = "/Users/turui/Desktop/excel-test.xlsx";
EasyExcel.read(fileName, MyLogEntity.class, new MyLogEntityListener()).sheet().doRead();
}
@Test
public void write() {
List<MyLogEntity> data = Lists.newArrayList();
for (int i = 0;i < 10; i++) {
MyLogEntity myLogEntity = new MyLogEntity();
myLogEntity.setDt("20190101");
data.add(myLogEntity);
}
String fileName = "/Users/turui/Desktop/test_write" + System.currentTimeMillis() + ".xlsx";
EasyExcel.write(fileName, MyLogEntity.class).sheet("模板1").doWrite(data);
}
}
提示的异常或者没有达到的效果 读写文件都会抛出异常
com.alibaba.excel.exception.ExcelGenerateException: java.lang.ExceptionInInitializerError
at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:67)
at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:161)
at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:146)
at com.alibaba.excel.write.builder.ExcelWriterSheetBuilder.doWrite(ExcelWriterSheetBuilder.java:190)
at com.meituan.rc.holmes.service.util.ExcelTest.write(ExcelTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ExceptionInInitializerError
at net.sf.cglib.beans.BeanMap.create(BeanMap.java:57)
at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addJavaObjectToExcel(ExcelWriteAddExecutor.java:114)
at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addOneRowOfDataToExcel(ExcelWriteAddExecutor.java:65)
at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.add(ExcelWriteAddExecutor.java:51)
at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:61)
... 26 more
Caused by: java.lang.IllegalStateException: Unable to load cache item
at net.sf.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:79)
at net.sf.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:221)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:174)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:157)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:149)
at net.sf.cglib.beans.BeanMap$Generator.