Apache POI XSSF reading in excel files

前端 未结 8 1713
粉色の甜心
粉色の甜心 2021-02-13 00:12

I just have a quick question about how to read in an xlsx file using the XSSF format from Apache.

Right now my code looks like this:

InputStream fs = ne         


        
8条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 00:39

    You can try the following.

    private static void readXLSX(String path) throws IOException {
        File myFile = new File(path);
        FileInputStream fis = new FileInputStream(myFile);
    
        // Finds the workbook instance for XLSX file
        XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
    
        // Return first sheet from the XLSX workbook
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
    
        // Get iterator to all the rows in current sheet
        Iterator rowIterator = mySheet.iterator();
    
        // Traversing over each row of XLSX file
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
    
            // For each row, iterate through each columns
            Iterator cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
    
                Cell cell = cellIterator.next();
    
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t");
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "\t");
                    break;
                default :
    
                }
            }
            System.out.println("");
        }
    }
    

提交回复
热议问题