【发布时间】:2023-03-27 11:24:01
【问题描述】:
我正在用 Java 中的 Eclipse 构建一个应用程序。我想读取 XLS 文件。
public Carico leggiOrdineDaFile_Mazzeo(Fornitore f,String file){
try{
FileInputStream inputStream = new FileInputStream(new File(file));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
Carico ordine = new Carico();
List<DettOrdini> listaArticoli = new ArrayList<DettOrdini>();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue());
break;
}
System.out.print(" - ");
}
System.out.println();
}
((BufferedReader) workbook).close();
inputStream.close();
}
但如果我尝试启动我的代码,我会收到以下错误消息:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
我在我的项目中导入了这个库:
poi-3.17.jar
poi-excelant-3.17.jar
poi-ooxml-3.17.jar
poi-ooxml-schemas-3.17.jar
poi-scratchpad-3.17.jar
xmlbeans-2.6.0.jar
curveaspi-1.0.4.jar
【问题讨论】:
-
它抱怨
NoClassDefFoundError的类应该位于org.apache.commons,但我们看不到您已将任何与org.apache.commons相关的内容导入到您的项目中。 -
错误信息提示您需要在类路径中包含Apache Commons Collections v4。请注意,this page about Apache POI 上的“组件映射”部分提到了使用 Apache POI 所需的先决条件。
-
@kenshinji 我已将 org.apache.commons 添加到我的项目中,然后我已经解决了我的问题