我的错误在哪里?

我有一个阅读Excel文件的问题,并通过Apache poi 3.9进行分析…我添加了外部的JAR文件,但它仍然给我错误。 在这里我的代码

import java.io.File; import java.io.FileInputStream; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class loop { public static void main(String [] args) throws Exception { File excel= new File("C:\\Users\\songSent.xlsx"); FileInputStream fis= new FileInputStream(excel); XSSFWorkbook wb= new XSSFWorkbook(fis); XSSFSheet ws= wb.getSheet("Input"); int rowNum=ws.getLastRowNum() +1; int colNum=ws.getRow(0).getLastCellNum(); String [][] data= new String[rowNum][colNum]; for(int i=0; i<rowNum; i++) { XSSFRow row= ws.getRow(i); for(int j=0; j<colNum; j++) { XSSFCell cell=row.getCell(j); String value=cellToString(cell); data[i][j]=value; System.out.println("the value is " +value); } } } public static String cellToString(XSSFCell cell) { int type; Object result; type=cell.getCellType(); switch (type){ case 0: result=cell.getNumericCellValue(); break; case 1: result=cell.getStringCellValue(); break; default: throw new RuntimeException("There no support"); } return result.toString(); } } 

这些是我运行程序时的错误

 Exception in thread "main" java.lang.NoClassDefFoundError:org/apache/poi/hssf/usermodel/HSSFCell at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod(Unknown Source) at sun.launcher.LauncherHelper.getMainMethod(Unknown Source) at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) Caused by: java.lang.ClassNotFoundException:org.apache.poi.hssf.usermodel.HSSFCell at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 6 more 

在运行时,至less缺less一些Apache POI jar 。 具体来说,你错过了主要的poi jar(例如poi-3.10-beta2-20130904.jar ),而且很可能还有一些其他的。

首先,您需要记住,使用Java,您的jar需要在编译时运行时可用。 仅仅在编译时在你的类path上放置jar是不够的,你还需要在运行时将它们放在类path上!

其次,您需要确保包含适用于您正在使用的POI组件的Apache POI jar以及这些组件所具有的任何依赖关系。 这些都logging在Apache POI组件页面上 ,这解释了哪些Jars拥有哪些组件,以及它们具有哪些依赖关系。

(因为你使用XSSF,这意味着你需要主poi jar,poi-ooxml jar,poi-ooxml-schemas jar和它们的依赖 )

从堆栈跟踪中可以清楚的看出,你的程序不能find“poi” jar。 检查你的类path是否设置正确,如果你是从eclipse(或其他IDE)运行检查是否jar添加到构buildpath。