简体   繁体   English

从java中读取excel文件时出错

[英]Error while reading from excel file in java

I am making an app in Android Studio, and I am quite new it.我正在 Android Studio 中制作一个应用程序,我很新。 When i run my app, it crashes.当我运行我的应用程序时,它崩溃了。 My goal is to read the cells out of an excel document.我的目标是从 excel 文档中读取单元格。 The excel document is in assets. Excel 文档在资产中。 Have tried converting to both an xls file, and an xlsx file.已尝试转换为 xls 文件和 xlsx 文件。 Cannot find any help on this error, or how to fix it.无法找到有关此错误或如何修复它的任何帮助。 This is the error:这是错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.pedikalk, PID: 10587
    java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/math3/util/ArithmeticUtils;
        at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)
        at org.apache.poi.poifs.property.DirectoryProperty.<init>(DirectoryProperty.java:52)
        at org.apache.poi.poifs.property.RootProperty.<init>(RootProperty.java:31)
        at org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:58)
        at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:102)
        at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:274)
        at com.example.pedikalk.Scanner.readExcelFileFromAssets(Scanner.java:40)
        at com.example.pedikalk.Scanner.<init>(Scanner.java:26)
        at com.example.pedikalk.MainActivity.onCreate(MainActivity.java:47)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        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)

This is my code:这是我的代码:

import android.content.res.AssetManager;
import android.util.Log;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

public class Scanner{

    IOException FileNotFoundException = new IOException();
    AssetManager assetManager;

    public Scanner(AssetManager asset) throws IOException {

        this.assetManager = asset;
        readExcelFileFromAssets();
    }

    public void readExcelFileFromAssets() { //:)

        try {
            // Creating Input Stream
            /*
             * File file = new File( filename); FileInputStream myInput = new
             * FileInputStream(file);
             */
            InputStream myInput= assetManager.open("Pedikalk matrise.xlsx");

            // Create a POIFSFileSystem object
            POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); //This is the line blamed for the error

            // Create a workbook using the File System
            HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

            // Get the first sheet from workbook
            HSSFSheet mySheet = myWorkBook.getSheetAt(0);

            // We now need something to iterate through the cells.
            Iterator<Row> rowIter = mySheet.rowIterator();
            while (rowIter.hasNext()) {
                HSSFRow myRow = (HSSFRow) rowIter.next();
                Iterator<Cell> cellIter = myRow.cellIterator();
                while (cellIter.hasNext()) {
                    HSSFCell myCell = (HSSFCell) cellIter.next();
                    System.out.println(myCell.toString());
                    Log.e("FileUtils", "Cell Value: " + myCell.toString()+ " Index :" +myCell.getColumnIndex());
                    // Toast.makeText(getApplicationContext(), "cell Value: " +
                    // myCell.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }

Can anyone help?任何人都可以帮忙吗?

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/math3/util/ArithmeticUtils;

The JVM is not able to locate ArithmeticUtils . JVM 无法找到ArithmeticUtils You can get that from Apache Commons Math.你可以从 Apache Commons Math 中得到它。

In case you are using maven, you can add this dependency to your pom.xml file如果您使用的是 maven,您可以将此依赖项添加到您的pom.xml文件中

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.0</version>
</dependency>

For other dependency managers, see https://mvnrepository.com/artifact/org.apache.commons/commons-math3/3.0对于其他依赖管理器,请参阅https://mvnrepository.com/artifact/org.apache.commons/commons-math3/3.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM