android – 使用Apache POI打开.xlsx文件时发生...

codeday· 2019-10-22
本文来自 codeday ,作者 codeday
我一直在研究一个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
最佳答案
JSpreadsheet是Java和Android的另一个API.它读取并创建.xlsx文件.没有依赖项,只有一个jar文件.