apache poi错误zip文件已关闭

时间:2014-09-20 06:20:37

标签: java android

我正在尝试使用apache poi libs打开excel .xlsx文件,但遇到下面的错误。

以下是发生错误的部分的代码。 xlsx文件存储在res / raw中。这里发生了什么导致错误?

            String folderdir = "android.resource://" + getPackageName() + "/" + R.raw.register_template;
//Workbook wb = WorkbookFactory.create(new File(folderdir));
        OPCPackage xlsx = OPCPackage.open(new File(folderdir));//must be .xlsx file type
        XSSFWorkbook xlsxwb = new XSSFWorkbook(xlsx);

        xlsx.save(jobno_file);
        xlsx.close();

错误代码:

   09-20 16:12:07.039    5694-5694/ E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: , PID: 5694
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3823)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: Zip File is closed
            at org.apache.poi.openxml4j.util.ZipFileZipEntrySource.getEntries(ZipFileZipEntrySource.java:45)
            at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:182)
            at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:665)
            at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:253)
            at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:200)
            at au.com.prensa.prensa.generalinfo.save_register(generalinfo.java:83)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

你可以确切地说出这出错的地方。您不能在这样的路径上使用File,因为它不是文件系统上文件的路径。如果通过提供InputStream无法调用unzipper,则必须先将res / raw中的'file'复制到文件系统。