Apache POI XSSF reading in excel files Apache POI XSSF reading in excel files apache apache

Apache POI XSSF reading in excel files


I bealive that this will answer your questions: http://poi.apache.org/spreadsheet/quick-guide.html#ReadWriteWorkbook

In short, your code should look like this:

InputStream inp = new FileInputStream("workbook.xlsx");Workbook wb = WorkbookFactory.create(inp);Sheet sheet = wb.getSheetAt(0);Row row = sheet.getRow(2);Cell cell = row.getCell(3);


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);            }        }


Why are you breaking the file into an InputStream? XSSFWorkbook has a constructor that simply takes the path as a String. Just hard code the path of the string in. Once you create the workbook you can create XSSFSheets from that. Then XSSFCells, which will then finally allow you to read the contents of a single cell (cells are based on x,y locations, essentially)