简体   繁体   中英

Error while reading from excel file in java

I am making an app in Android Studio, and I am quite new it. When i run my app, it crashes. My goal is to read the cells out of an excel document. The excel document is in assets. Have tried converting to both an xls file, and an xlsx file. 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 . You can get that from Apache Commons Math.

In case you are using maven, you can add this dependency to your pom.xml file

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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