如何从Java读取xlsx文件

时间:2017-11-24 09:18:42

标签: java xlsx

我正在用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

1 个答案:

答案 0 :(得分:0)

正如您的错误消息所示:您只是错过了{{1}}。

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