java - 使用poi API读取XLSX文件不起作用

标签 java android excel

所以我得到了这个 Excel 文件,我想从中读取一些数据,并且我发现 Apache POI api 似乎可以满足我的要求。不过我有一点小问题。我的 Gradle Sync 看起来不错,但每当我尝试运行该应用程序时,我都会收到以下错误消息:

Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

   AGPBI: {"kind":"error","text":"trouble processing \"javax/xml/XMLConstants.class\":","sources":[{}]}
AGPBI: {"kind":"error","text":"Ill-advised or mistaken usage of a core class (java.* or javax.*)","sources":[{}]}
AGPBI: {"kind":"error","text":"when not building a core library.","sources":[{}]}
AGPBI: {"kind":"error","text":"This is often due to inadvertently including a core library file","sources":[{}]}
AGPBI: {"kind":"error","text":"in your application\u0027s project, when using an IDE (such as","sources":[{}]}
AGPBI: {"kind":"error","text":"Eclipse). If you are sure you\u0027re not intentionally defining a","sources":[{}]}
AGPBI: {"kind":"error","text":"core class, then this is the most likely explanation of what\u0027s","sources":[{}]}
AGPBI: {"kind":"error","text":"going on.","sources":[{}]}
AGPBI: {"kind":"error","text":"However, you might actually be trying to define a class in a core","sources":[{}]}
AGPBI: {"kind":"error","text":"namespace, the source of which you may have taken, for example,","sources":[{}]}
AGPBI: {"kind":"error","text":"from a non-Android virtual machine project. This will most","sources":[{}]}
AGPBI: {"kind":"error","text":"assuredly not work. At a minimum, it jeopardizes the","sources":[{}]}
AGPBI: {"kind":"error","text":"compatibility of your app with future versions of the platform.","sources":[{}]}
AGPBI: {"kind":"error","text":"It is also often of questionable legality.","sources":[{}]}
AGPBI: {"kind":"error","text":"If you really intend to build a core library -- which is only","sources":[{}]}
AGPBI: {"kind":"error","text":"appropriate as part of creating a full virtual machine","sources":[{}]}
AGPBI: {"kind":"error","text":"distribution, as opposed to compiling an application -- then use","sources":[{}]}
AGPBI: {"kind":"error","text":"the \"--core-library\" option to suppress this error message.","sources":[{}]}
AGPBI: {"kind":"error","text":"If you go ahead and use \"--core-library\" but are in fact","sources":[{}]}
AGPBI: {"kind":"error","text":"building an application, then be forewarned that your application","sources":[{}]}
AGPBI: {"kind":"error","text":"will still fail to build or run, at some point. Please be","sources":[{}]}
AGPBI: {"kind":"error","text":"prepared for angry customers who find, for example, that your","sources":[{}]}
AGPBI: {"kind":"error","text":"application ceases to function once they upgrade their operating","sources":[{}]}
AGPBI: {"kind":"error","text":"system. You will be to blame for this problem.","sources":[{}]}
AGPBI: {"kind":"error","text":"If you are legitimately using some code that happens to be in a","sources":[{}]}
AGPBI: {"kind":"error","text":"core package, then the easiest safe alternative you have is to","sources":[{}]}
AGPBI: {"kind":"error","text":"repackage that code. That is, move the classes in question into","sources":[{}]}
AGPBI: {"kind":"error","text":"your own package namespace. This means that they will never be in","sources":[{}]}
AGPBI: {"kind":"error","text":"conflict with core system classes. JarJar is a tool that may help","sources":[{}]}
AGPBI: {"kind":"error","text":"you in this endeavor. If you find that you cannot do this, then","sources":[{}]}
AGPBI: {"kind":"error","text":"that is an indication that the path you are on will ultimately","sources":[{}]}
AGPBI: {"kind":"error","text":"lead to pain, suffering, grief, and lamentation.","sources":[{}]}
AGPBI: {"kind":"error","text":"1 error; aborting","sources":[{}]}

 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException:

Error:trouble processing "javax/xml/XMLConstants.class":
Error:Ill-advised or mistaken usage of a core class (java.* or javax.*)
Error:when not building a core library.
Error:This is often due to inadvertently including a core library file
Error:in your application's project, when using an IDE (such as
Error:Eclipse). If you are sure you're not intentionally defining a
Error:core class, then this is the most likely explanation of what's
Error:going on.
Error:However, you might actually be trying to define a class in a core
Error:namespace, the source of which you may have taken, for example,
Error:from a non-Android virtual machine project. This will most
Error:assuredly not work. At a minimum, it jeopardizes the
Error:compatibility of your app with future versions of the platform.
Error:It is also often of questionable legality.
Error:If you really intend to build a core library -- which is only
Error:appropriate as part of creating a full virtual machine
Error:distribution, as opposed to compiling an application -- then use
Error:the "--core-library" option to suppress this error message.
Error:If you go ahead and use "--core-library" but are in fact
Error:building an application, then be forewarned that your application
Error:will still fail to build or run, at some point. Please be
Error:prepared for angry customers who find, for example, that your
Error:application ceases to function once they upgrade their operating
Error:system. You will be to blame for this problem.
Error:If you are legitimately using some code that happens to be in a
Error:core package, then the easiest safe alternative you have is to
Error:repackage that code. That is, move the classes in question into
Error:your own package namespace. This means that they will never be in
Error:conflict with core system classes. JarJar is a tool that may help
Error:you in this endeavor. If you find that you cannot do this, then
Error:that is an indication that the path you are on will ultimately
Error:lead to pain, suffering, grief, and lamentation.
Error:1 error; aborting
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_51\bin\java.exe'' finished with non-zero exit value 1

我尝试了两种方法来添加依赖项。首先,我从他们的网站下载了所有 jar 文件。将它们放在 libs 文件夹中,并在 gradle 文件中添加文件树依赖项。这不起作用。现在我只是用这个来代替:

compile 'org.apache.poi:poi:3.14'
compile 'org.apache.poi:poi-ooxml:3.14'
compile 'org.apache.poi:poi-ooxml-schemas:3.14'
compile 'org.apache.xmlbeans:xmlbeans:2.6.0'
compile 'com.github.virtuald:curvesapi:1.03'

//这是实际代码:

private void XLSFileReader(){
                    try {
                        FileInputStream file = new FileInputStream(new File(xlsFilePath));

                        //Create Workbook instance holding reference to .xlsx file
                        XSSFWorkbook workbook = new XSSFWorkbook(file);

                        //Get first/desired sheet from the workbook
                        XSSFSheet sheet = workbook.getSheetAt(0);

                        //Iterate through each rows one by one
                        Iterator<Row> rowIterator = sheet.iterator();
                        while (rowIterator.hasNext())
                        {
                            Row row = rowIterator.next();

                            //For each row, iterate through all the columns
                            Iterator<Cell> cellIterator = row.cellIterator();

                            while (cellIterator.hasNext())
                            {
                                Cell cell = cellIterator.next();
                                //Check the cell type and format accordingly
                                switch (cell.getCellType())
                                {
                    //Logs to the window to confirm that it works
                    case Cell.CELL_TYPE_NUMERIC:
                        Log.d("Kolumn", cell.getNumericCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_STRING:
                        Log.d("Rad",cell.getStringCellValue() + "\t");
                        break;
                }
            }
            System.out.println("");
        }
        file.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

我进行了很多搜索,但没有找到对我有用的方法。

最佳答案

阅读这篇文章 - http://blog.kondratev.pro/2015/08/reading-xlsx-on-android-4-and-hopefully.html问题出在xmlbeans-2.6.0.jar

关于java - 使用poi API读取XLSX文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36557050/

相关文章:

excel - 查找文本并更改颜色

处理文件上传时出现java.lang.NullPointerException

java - Break 语句会导致执行速度变慢?

java - Hibernate一级缓存的使用

java - 如何通知 MediaStore 某些项目已删除?

android - 在android中获取联系人照片的文件系统路径

excel - 错误处理在Error.Number 3021上无法按预期工作

java - 从 jpanel 获取 (g) 图形

java - 在我返回 Android 项目中的 Activity 之前,变量不会得到更新

excel - 为 Microsoft Excel 加载项分配键盘快捷键