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

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

Error while reading from excel file in java

问题

  1. 我正在使用Android Studio制作一个应用程序但我对此还很陌生当我运行我的应用程序时它会崩溃我的目标是从Excel文档中读取单元格内容Excel文档位于assets文件夹中我尝试过将其转换为xls文件和xlsx文件但无法找到关于此错误或如何修复它的任何帮助信息以下是错误信息
  2. E/AndroidRuntime: FATAL EXCEPTION: main
  3. Process: com.example.pedikalkPID: 10587
  4. java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/math3/util/ArithmeticUtils;
  5. at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)
  6. at org.apache.poi.poifs.property.DirectoryProperty.<init>(DirectoryProperty.java:52)
  7. at org.apache.poi.poifs.property.RootProperty.<init>(RootProperty.java:31)
  8. at org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:58)
  9. at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:102)
  10. at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:274)
  11. at com.example.pedikalk.Scanner.readExcelFileFromAssets(Scanner.java:40)
  12. at com.example.pedikalk.Scanner.<init>(Scanner.java:26)
  13. at com.example.pedikalk.MainActivity.onCreate(MainActivity.java:47)
  14. at android.app.Activity.performCreate(Activity.java:8000)
  15. at android.app.Activity.performCreate(Activity.java:7984)
  16. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
  17. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
  18. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
  19. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
  20. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
  21. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
  22. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
  23. at android.os.Handler.dispatchMessage(Handler.java:106)
  24. at android.os.Looper.loop(Looper.java:223)
  25. at android.app.ActivityThread.main(ActivityThread.java:7656)
  26. at java.lang.reflect.Method.invoke(Native Method)
  27. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
  28. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
  29. 这是我的代码
  30. import android.content.res.AssetManager;
  31. import android.util.Log;
  32. import org.apache.poi.hssf.usermodel.HSSFCell;
  33. import org.apache.poi.hssf.usermodel.HSSFRow;
  34. import org.apache.poi.hssf.usermodel.HSSFSheet;
  35. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  36. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  37. import org.apache.poi.ss.usermodel.Cell;
  38. import org.apache.poi.ss.usermodel.Row;
  39. import java.io.IOException;
  40. import java.io.InputStream;
  41. import java.util.Iterator;
  42. public class Scanner{
  43. IOException FileNotFoundException = new IOException();
  44. AssetManager assetManager;
  45. public Scanner(AssetManager asset) throws IOException {
  46. this.assetManager = asset;
  47. readExcelFileFromAssets();
  48. }
  49. public void readExcelFileFromAssets() { //:)
  50. try {
  51. // 创建输入流
  52. /*
  53. * File file = new File( filename); FileInputStream myInput = new
  54. * FileInputStream(file);
  55. */
  56. InputStream myInput= assetManager.open("Pedikalk matrise.xlsx");
  57. // 创建一个POIFSFileSystem对象
  58. POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); // 这是被指责出错的那一行
  59. // 使用文件系统创建工作簿
  60. HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
  61. // 从工作簿获取第一个表格
  62. HSSFSheet mySheet = myWorkBook.getSheetAt(0);
  63. // 我们现在需要遍历单元格
  64. Iterator<Row> rowIter = mySheet.rowIterator();
  65. while (rowIter.hasNext()) {
  66. HSSFRow myRow = (HSSFRow) rowIter.next();
  67. Iterator<Cell> cellIter = myRow.cellIterator();
  68. while (cellIter.hasNext()) {
  69. HSSFCell myCell = (HSSFCell) cellIter.next();
  70. System.out.println(myCell.toString());
  71. Log.e("FileUtils", "Cell Value: " + myCell.toString() + " Index :" + myCell.getColumnIndex());
  72. // Toast.makeText(getApplicationContext(), "cell Value: " +
  73. // myCell.toString(), Toast.LENGTH_SHORT).show();
  74. }
  75. }
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. return;
  80. }
  81. }
  82. 有谁能帮忙解决吗
英文:

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:

  1. E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.example.pedikalk, PID: 10587
  3. java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/math3/util/ArithmeticUtils;
  4. at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)
  5. at org.apache.poi.poifs.property.DirectoryProperty.&lt;init&gt;(DirectoryProperty.java:52)
  6. at org.apache.poi.poifs.property.RootProperty.&lt;init&gt;(RootProperty.java:31)
  7. at org.apache.poi.poifs.property.PropertyTable.&lt;init&gt;(PropertyTable.java:58)
  8. at org.apache.poi.poifs.filesystem.POIFSFileSystem.&lt;init&gt;(POIFSFileSystem.java:102)
  9. at org.apache.poi.poifs.filesystem.POIFSFileSystem.&lt;init&gt;(POIFSFileSystem.java:274)
  10. at com.example.pedikalk.Scanner.readExcelFileFromAssets(Scanner.java:40)
  11. at com.example.pedikalk.Scanner.&lt;init&gt;(Scanner.java:26)
  12. at com.example.pedikalk.MainActivity.onCreate(MainActivity.java:47)
  13. at android.app.Activity.performCreate(Activity.java:8000)
  14. at android.app.Activity.performCreate(Activity.java:7984)
  15. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
  16. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
  17. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
  18. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
  19. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
  20. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
  21. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
  22. at android.os.Handler.dispatchMessage(Handler.java:106)
  23. at android.os.Looper.loop(Looper.java:223)
  24. at android.app.ActivityThread.main(ActivityThread.java:7656)
  25. at java.lang.reflect.Method.invoke(Native Method)
  26. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
  27. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

This is my code:

  1. import android.content.res.AssetManager;
  2. import android.util.Log;
  3. import org.apache.poi.hssf.usermodel.HSSFCell;
  4. import org.apache.poi.hssf.usermodel.HSSFRow;
  5. import org.apache.poi.hssf.usermodel.HSSFSheet;
  6. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  7. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  8. import org.apache.poi.ss.usermodel.Cell;
  9. import org.apache.poi.ss.usermodel.Row;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.util.Iterator;
  13. public class Scanner{
  14. IOException FileNotFoundException = new IOException();
  15. AssetManager assetManager;
  16. public Scanner(AssetManager asset) throws IOException {
  17. this.assetManager = asset;
  18. readExcelFileFromAssets();
  19. }
  20. public void readExcelFileFromAssets() { //:)
  21. try {
  22. // Creating Input Stream
  23. /*
  24. * File file = new File( filename); FileInputStream myInput = new
  25. * FileInputStream(file);
  26. */
  27. InputStream myInput= assetManager.open(&quot;Pedikalk matrise.xlsx&quot;);
  28. // Create a POIFSFileSystem object
  29. POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); //This is the line blamed for the error
  30. // Create a workbook using the File System
  31. HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
  32. // Get the first sheet from workbook
  33. HSSFSheet mySheet = myWorkBook.getSheetAt(0);
  34. // We now need something to iterate through the cells.
  35. Iterator&lt;Row&gt; rowIter = mySheet.rowIterator();
  36. while (rowIter.hasNext()) {
  37. HSSFRow myRow = (HSSFRow) rowIter.next();
  38. Iterator&lt;Cell&gt; cellIter = myRow.cellIterator();
  39. while (cellIter.hasNext()) {
  40. HSSFCell myCell = (HSSFCell) cellIter.next();
  41. System.out.println(myCell.toString());
  42. Log.e(&quot;FileUtils&quot;, &quot;Cell Value: &quot; + myCell.toString()+ &quot; Index :&quot; +myCell.getColumnIndex());
  43. // Toast.makeText(getApplicationContext(), &quot;cell Value: &quot; +
  44. // myCell.toString(), Toast.LENGTH_SHORT).show();
  45. }
  46. }
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. return;
  51. }

Can anyone help?

答案1

得分: 1

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

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
  3. &lt;artifactId&gt;commons-math3&lt;/artifactId&gt;
  4. &lt;version&gt;3.0&lt;/version&gt;
  5. &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:

确定