2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

read all the values from a excel sheet using Apache POI?

I want to read all cell value from excel sheet using Apache POI and store it into one dimensional string array .Is is possible to read data from excel row by row and extract value from each cell?

Please help me to solve this problem.

userimage

Here is the solution for you,


String fileName = "D:/ExcelFile.xls";
POIFSFileSystem fileSystem = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
HSSFSheet sheet = workbook.getSheetAt(0); //Get Excel Sheet

//Iterate throw each row.
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
    HSSFRow row = (HSSFRow) rows.next();

    Iterator<Cell> cells = row.cellIterator();
    while (cells.hasNext()) {
        HSSFCell cell = (HSSFCell) cells.next();

        //Must do this, you need to get value based on the cell type
        switch (cell.getCellType()) {
            case HSSFCell.CELL_TYPE_NUMERIC:
                System.out.println(cell.getNumericCellValue());
            break;

            case HSSFCell.CELL_TYPE_STRING:
                System.out.printlncell.getStringCellValue());
            break;

            default: break;
        }
    }
}

Answer is