使用Apache POI打开.xlsx文件时发生异常

时间:2013-07-05 09:16:31

标签: android file exception-handling apache-poi

我一直在研究一个Android应用程序,我必须打开.xls和.xlsx文件才能进一步实现。在.xls的情况下一切正常,但是当我尝试打开.xls x文件时,它给了我异常。下面是我的代码 请帮我解决这些异常

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_code  && resultCode == RESULT_OK && null != data) {
            String FileName = data.getData().getLastPathSegment();

            String FilePath = data.getData().getPath();
            if(FilePath.endsWith(".xlsx")){
try{
                    FileInputStream file = new FileInputStream(new File(FilePath));
                    XSSFWorkbook myWorkBook2 = new XSSFWorkbook(file);
                    XSSFSheet mySheet2 = myWorkBook2.getSheetAt(0);}
                catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 


                }

我使用的罐子:

  • DOM4J-1.6.1.jar
  • log4j-1.2.13.jar
  • POI-3.9-20121203.jar
  • POI-OOXML-3.9-20121203.jar
  • 的xmlbeans-2.3.0.jar

Logcat错误

07-05 14:09:31.654: E/AndroidRuntime(6774): FATAL EXCEPTION: main
07-05 14:09:31.654: E/AndroidRuntime(6774): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=android.intent.action.GET_CONTENT cat=[android.intent.category.OPENABLE] dat=file:///sdcard/test .xlsx flg=0x3000000 cmp=miyowa.android.microsoft.wlm/com.miyowa.android.framework.ui.miyowaExplorer.MiyowaExplorer (has extras) }} to activity {com.example.converter/com.example.converter.Homepage}: org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
07-05 14:09:31.654: E/AndroidRuntime(6774):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3142)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3185)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at android.app.ActivityThread.access$1100(ActivityThread.java:134)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at android.os.Looper.loop(Looper.java:137)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at android.app.ActivityThread.main(ActivityThread.java:4666)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at java.lang.reflect.Method.invoke(Method.java:511)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at dalvik.system.NativeStart.main(Native Method)
07-05 14:09:31.654: E/AndroidRuntime(6774): Caused by: org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
07-05 14:09:31.654: E/AndroidRuntime(6774):     at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:403)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:207)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at com.example.converter.Homepage.onActivityResult(Homepage.java:358)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at android.app.Activity.dispatchActivityResult(Activity.java:4694)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3138)
07-05 14:09:31.654: E/AndroidRuntime(6774):     ... 11 more
07-05 14:09:31.654: E/AndroidRuntime(6774): Caused by: java.lang.reflect.InvocationTargetException
07-05 14:09:31.654: E/AndroidRuntime(6774):     at java.lang.reflect.Constructor.constructNative(Native Method)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-05 14:09:31.654: E/AndroidRuntime(6774):     at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)
07-05 14:09:31.654: E/AndroidRuntime(6774):     ... 17 more
07-05 14:09:31.654: E/AndroidRuntime(6774): Caused by: java.lang.NoClassDefFoundError: org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory
07-05 14:09:31.654: E/AndroidRuntime(6774):     at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:44)
07-05 14:09:31.654: E/AndroidRuntime(6774):     ... 20 more

2 个答案:

答案 0 :(得分:3)

请参阅Apache POI Components and Dependencies page - 您缺少poi-ooxml-schemas文件,这就是您为模式类获取类找不到异常的原因

答案 1 :(得分:0)

JSpreadsheet是Java和Android的另一个API。它读取并创建.xlsx文件。没有依赖项,只有一个jar文件。