【发布时间】:2021-11-24 19:54:43
【问题描述】:
当我尝试加载 xls 文件时,即使在同一个文件夹中,它也无法正常工作。我也尝试过使用绝对路径。 (这一切都是因为缺少罐子而发生的,这里是所有罐子的列表。为了解决相对路径的问题,下面的 url.getResource() 可以正常工作。) My Jar List(Image)
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream f = new FileInputStream("MP.xls");
HSSFWorkbook l = new HSSFWorkbook(f);
}
}
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream f = new FileInputStream("C:\\Users\\alumno.Alumno-PC\\Documents\\NetBeansProjects\\pruebas xlsx\\src\\pruebasxlsx\\MP.xls");
HSSFWorkbook l = new HSSFWorkbook(f);
}
}
This is where the file is located(picture)
这是相对路径的错误:
Exception in thread "main" java.io.FileNotFoundException: MP.xls (El sistema no puede encontrar el archivo especificado)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
at pruebasxlsx.Main.main(Main.java:22)
这是绝对路径的错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/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:99)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:272)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:399)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:381)
at pruebasxlsx.Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 9 more
【问题讨论】:
-
请这样试试,FileInputStream file = new FileInputStream(new File("*file_path\\MP.xls"));
-
使用相对路径会弹出相同的错误,使用绝对路径会出现另一个错误。但仍然无法正常工作
-
能否请您添加您尝试过的两种方法以及发现的相关错误?
-
绝对路径是什么?您可以通过在 cmd.exe 中执行以下操作来尝试:
dir C:\Users\foo\bar\MP.xlx -
提供文件扩展名 .xlsx 而不是 .xls 它将起作用