OutputStream os = null;
try {
os = getContentResolver().openOutputStream(insert);
// os.write(100);
ExcelUtils.createExcelTemplate(os, DemoBean.class, "testSheet");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// ExcelUtils里简单封装的方法:
public static <T> void createExcelTemplate(OutputStream os, Class<T> clazz, String sheetName) {
EasyExcel.write(os, clazz)
.excelType(ExcelTypeEnum.XLS)
.useDefaultStyle(false)
.sheet(sheetName)
.doWrite(data());
// .doWrite((Collection<?>) null);
}
private static List<DemoBean> data() {
List<DemoBean> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
DemoBean data = new DemoBean();
data.setShelfName("字符串" + i);
data.setShelfCode(new Date().toString());
data.setRemark(String.valueOf(0.56 + i));
list.add(data);
}
return list;
}
异常提示
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.szcti.importorexprotexceltest, PID: 7931
com.alibaba.excel.exception.ExcelGenerateException: java.lang.ExceptionInInitializerError
at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:65)
at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:70)
at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:47)
at com.alibaba.excel.write.builder.ExcelWriterSheetBuilder.doWrite(ExcelWriterSheetBuilder.java:62)
at com.szcti.importorexprotexceltest.uitl.ExcelUtils.createExcelTemplate(ExcelUtils.java:36)
at com.szcti.importorexprotexceltest.MainActivity.insert(MainActivity.java:92)
at com.szcti.importorexprotexceltest.MainActivity.onClick(MainActivity.java:67)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.ExceptionInInitializerError
at com.alibaba.excel.util.BeanMapUtils.create(BeanMapUtils.java:25)
at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addJavaObjectToExcel(ExcelWriteAddExecutor.java:144)
at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addOneRowOfDataToExcel(ExcelWriteAddExecutor.java:82)
at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.add(ExcelWriteAddExecutor.java:58)
at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:59)
at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:70)
at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:47)
at com.alibaba.excel.write.builder.ExcelWriterSheetBuilder.doWrite(ExcelWriterSheetBuilder.java:62)
at com.szcti.importorexprotexceltest.uitl.ExcelUtils.createExcelTemplate(ExcelUtils.java:36)
at com.szcti.importorexprotexceltest.MainActivity.insert(MainActivity.java:92)
at com.szcti.importorexprotexceltest.MainActivity.onClick(MainActivity.java:67)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: com.alibaba.excel.support.cglib.core.CodeGenerationException: java.lang.UnsupportedOperationException-->can't load this type of class file
at com.alibaba.excel.support.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:558)
at com.alibaba.excel.support.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363)
at com.alibaba.excel.support.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:110)
at com.alibaba.excel.support.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:108)
at com.alibaba.excel.support.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.alibaba.excel.support.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
at com.alibaba.excel.support.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
at com.alibaba.excel.support.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:134)
at com.alibaba.excel.support.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:319)
at com.alibaba.excel.support.cglib.core.KeyFactory$Generator.create(KeyFactory.java:237)
at com.alibaba.excel.support.cglib.core.KeyFactory.create(KeyFactory.java:184)
at com.alibaba.excel.support.cglib.core.KeyFactory.create(KeyFactory.java:164)
at com.alibaba.excel.support.cglib.core.KeyFactory.create(KeyFactory.java:156)
at com.alibaba.excel.support.cglib.beans.BeanMap$Generator.
建议描述 这个代码是我在Android端写的,单独的测试ExcelUtils里的方法,创建excel,读取excel测试都没有问题,但是在Android端,创建一个空的excel表格,不调用data()填充数据,可以正常跑,只要调用了data()填充数据,就会报上述错误,读取excel里的数据也是上述错误,请问这是什么原因?创建空的excel,第一行head和实体类中用@ExcelProperty标注的顺序是相反的