//..
We will read above xls file and prints the data.
try {
   Â
    FileInputStream file = new FileInputStream(new File("C:\test.xls"));
   Â
    //Get the workbook instance for XLS file
    HSSFWorkbook workbook = new HSSFWorkbook(file);
Â
    //Get first sheet from the workbook
    HSSFSheet sheet = workbook.getSheetAt(0);
   Â
    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheet.iterator();
    while(rowIterator.hasNext()) {
        Row row = rowIterator.next();
       Â
        //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        while(cellIterator.hasNext()) {
           Â
            Cell cell = cellIterator.next();
           Â
            switch(cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "tt");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "tt");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "tt");
                    break;
            }
        }
        System.out.println("");
    }
    file.close();
    FileOutputStream out =
        new FileOutputStream(new File("C:\test.xls"));
    workbook.write(out);
    out.close();
   Â
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Â
Output
Emp Id   Name       Salary    Â
1.0    John       2000000.0     Â
2.0    Dean       420000.0      Â
3.0    Sam    280000.0      Â
4.0    Cass       6000000.0 Â
Â
Hope this helps you.
Â
Thanks,
Â
|
 |