[alibaba/easyexcel]在Android端使用这个框架,写入数据和读取数据,都会出现如下错误,创建只有head的空表没有问题

2023-12-17 207 views
7
   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.(BeanMap.java:66) 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: java.lang.UnsupportedOperationException: can't load this type of class file at java.lang.ClassLoader.defineClass(ClassLoader.java:591) at java.lang.reflect.Method.invoke(Native Method) at com.alibaba.excel.support.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:555) 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.(BeanMap.java:66)  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)  I/Process: Sending signal. PID: 7931 SIG: 9

建议描述 这个代码是我在Android端写的,单独的测试ExcelUtils里的方法,创建excel,读取excel测试都没有问题,但是在Android端,创建一个空的excel表格,不调用data()填充数据,可以正常跑,只要调用了data()填充数据,就会报上述错误,读取excel里的数据也是上述错误,请问这是什么原因?创建空的excel,第一行head和实体类中用@ExcelProperty标注的顺序是相反的

回答

3

这个能否 加群联系群主,安卓真不懂,可以的话 一起搞定下

6

不好意思,才看到您的回复,很高兴您能回复我,非常感谢!这个问题可能是Android系统的问题,Android现在正逐步限制使用反射,认为这样是不安全的。我尝试过加群,但是验证的问题让我有点摸不着头脑,总是回答错误。我稍后会再次进行尝试,期待能够有机会一起解决这个问题。

6

java9曾经就想限制反射,但是由于影响太大放弃了 。 使用反射的框架实在太多了。 如果没有这个特性Android应该是不能适配的