英文:
Error while reading from excel file in java
问题
我正在使用Android Studio制作一个应用程序,但我对此还很陌生。当我运行我的应用程序时,它会崩溃。我的目标是从Excel文档中读取单元格内容。Excel文档位于assets文件夹中。我尝试过将其转换为xls文件和xlsx文件,但无法找到关于此错误或如何修复它的任何帮助信息。以下是错误信息:
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)
这是我的代码:
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.<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?
答案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
<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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论