Apache POI XSSF reading in excel files

前端 未结 8 1712
粉色の甜心
粉色の甜心 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:42
    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);
                }
            }
    
    0 讨论(0)
  • 2021-02-13 00:44
    // Load sheet- Here we are loading first sheet only
    XSSFSheet xlSheet = wb.getSheetAt(0);
    

    Here code recognizing only first sheet - In my Excel multiple sheets are there so facing problem to change sheet2 to sheet1(getSheetAt(0))...

    0 讨论(0)
提交回复
热议问题