Apache POI XSSF reading in excel files

前端 未结 8 2091
北恋
北恋 2021-02-13 00:00

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:27

    InputStream inp = null;
            try {
                inp = new FileInputStream("E:/sample_poi.xls");
    
                Workbook wb = WorkbookFactory.create(inp);
                Sheet sheet = wb.getSheetAt(0);
                Header header = sheet.getHeader();
    
                int rowsCount = sheet.getLastRowNum();
                System.out.println("Total Number of Rows: " + (rowsCount + 1));
                for (int i = 0; i <= rowsCount; i++) {
                    Row row = sheet.getRow(i);
                    int colCounts = row.getLastCellNum();
                    System.out.println("Total Number of Cols: " + colCounts);
                    for (int j = 0; j < colCounts; j++) {
                        Cell cell = row.getCell(j);
                        System.out.println("[" + i + "," + j + "]=" + cell.getStringCellValue());
                    }
                }
    
            } catch (Exception ex) {
                java.util.logging.Logger.getLogger(FieldController.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    inp.close();
                } catch (IOException ex) {
                    java.util.logging.Logger.getLogger(FieldController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    

提交回复
热议问题