【问题标题】:How to read xlsx file from Java如何从 Java 中读取 xlsx 文件
【发布时间】: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 添加到我的项目中,然后我已经解决了我的问题

标签: java xlsx


【解决方案1】:

正如您的错误消息所示:您只是缺少import org.apache.commons

参考https://commons.apache.org/proper/commons-collections/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2021-03-20
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多