在Java中从Excel文件读取时发生错误。

huangapple go评论87阅读模式
英文:

Error while reading from excel file in java

问题

我正在使用Android Studio制作一个应用程序但我对此还很陌生当我运行我的应用程序时它会崩溃我的目标是从Excel文档中读取单元格内容Excel文档位于assets文件夹中我尝试过将其转换为xls文件和xlsx文件但无法找到关于此错误或如何修复它的任何帮助信息以下是错误信息

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.pedikalkPID: 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)

这是我的代码

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 {
            // 创建输入流
            /*
             * File file = new File( filename); FileInputStream myInput = new
             * FileInputStream(file);
             */
            InputStream myInput= assetManager.open("Pedikalk matrise.xlsx");

            // 创建一个POIFSFileSystem对象
            POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); // 这是被指责出错的那一行

            // 使用文件系统创建工作簿
            HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

            // 从工作簿获取第一个表格
            HSSFSheet mySheet = myWorkBook.getSheetAt(0);

            // 我们现在需要遍历单元格
            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;
    }
}
有谁能帮忙解决吗
英文:

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.&lt;init&gt;(DirectoryProperty.java:52)
at org.apache.poi.poifs.property.RootProperty.&lt;init&gt;(RootProperty.java:31)
at org.apache.poi.poifs.property.PropertyTable.&lt;init&gt;(PropertyTable.java:58)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.&lt;init&gt;(POIFSFileSystem.java:102)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.&lt;init&gt;(POIFSFileSystem.java:274)
at com.example.pedikalk.Scanner.readExcelFileFromAssets(Scanner.java:40)
at com.example.pedikalk.Scanner.&lt;init&gt;(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(&quot;Pedikalk matrise.xlsx&quot;);
// 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&lt;Row&gt; rowIter = mySheet.rowIterator();
while (rowIter.hasNext()) {
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator&lt;Cell&gt; cellIter = myRow.cellIterator();
while (cellIter.hasNext()) {
HSSFCell myCell = (HSSFCell) cellIter.next();
System.out.println(myCell.toString());
Log.e(&quot;FileUtils&quot;, &quot;Cell Value: &quot; + myCell.toString()+ &quot; Index :&quot; +myCell.getColumnIndex());
// Toast.makeText(getApplicationContext(), &quot;cell Value: &quot; +
// myCell.toString(), Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}

Can anyone help?

答案1

得分: 1


JVM 无法定位 `ArithmeticUtils`。您可以从 Apache Commons Math 获取该类。
如果您使用 Maven,可以将以下依赖项添加到您的 `pom.xml` 文件中:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.0</version>
</dependency>
对于其他依赖管理器,请参阅 https://mvnrepository.com/artifact/org.apache.commons/commons-math3/3.0
英文:
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

&lt;dependency&gt;
&lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
&lt;artifactId&gt;commons-math3&lt;/artifactId&gt;
&lt;version&gt;3.0&lt;/version&gt;
&lt;/dependency&gt;

For other dependency managers, see https://mvnrepository.com/artifact/org.apache.commons/commons-math3/3.0

huangapple
  • 本文由 发表于 2020年9月30日 22:10:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64139393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定