我有一个函数,它接受 Excel 路径和工作表名称并返回行数的 int 值,但是当我尝试运行它时,它给出以下错误。我是java新手,所以可能是因为我太愚蠢了哈哈。
我的系统:
- Netbeans 8.2
- jdk 1.8
- Apache poi-src-4.1.2-20200217
- Kubuntu 最新
我的输入:
- ExcelPath:/media/bigdata/Downloads/text2.xls
- 工作表名称:工作表1
代码:
package com.test.system;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class NewClass {
private static FileInputStream fis;
private static FileOutputStream fileOut;
private static XSSFWorkbook wb;
private static XSSFSheet sh;
private static XSSFCell cell;
private static XSSFRow row;
private static XSSFCellStyle cellstyle;
private static XSSFColor mycolor;
public static int setExcelFile(String ExcelPath,String SheetName) throws Exception
{
int noOfRows = -1;
try{
File f = new File(ExcelPath);
if(!f.exists()){
System.out.println("File doesn't exist.");
}
else{
fis=new FileInputStream(ExcelPath);
System.out.println(fis);
wb=new XSSFWorkbook(fis);
sh = wb.getSheet(SheetName);
//sh = wb.getSheetAt(0); //0 - index of 1st sheet
if (sh == null)
{
sh = wb.createSheet(SheetName);
}
}
noOfRows = sh.getLastRowNum();
}catch (IOException e){System.out.println(e.getMessage());}
return (noOfRows);
}
}
错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
at com.test.system.NewClass.setExcelFile(NewClass.java:45)
at com.test.system.NewMain.main(NewMain.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 2 more
我尝试了一切,但仍然没有成功。
最佳答案
在 else 语句中你应该写
fis=new FileInputStream(f);
而不是
fis=new FileInputStream(ExcelPath);
并且您不能使用:System.out.println(fis);//这行不通
关于java - 使用 apache POI 和 java 访问 excel 数据时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60943504/