Wednesday, November 19, 2014

Java : Read from Excel (xls) file

Java : Reaf from Excel (xls) file

Reading from a text file is common. But, reading from an excel(.xls) file is not.
Recently, I got to do so. Little bit Google search and experimentation resulted in the following.

I used HSSF from Apache-POI.
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public void readFromXlsFile (String pathToFile, String sheetName){
    try {
        FileInputStream file = new FileInputStream(pathToFile);

        HSSFWorkbook workbook = new HSSFWorkbook(file);
        HSSFSheet sheet = workbook.getSheet(sheetName);

        Iterator<Row> rowIterator = sheet.rowIterator();
        while (rowIterator.hasNext()) {
            Row row =;

            if (row.getRowNum() == 0) {
                continue;// skip first row, as it contains column names

            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell =;
                if (cell.getColumnIndex() == 0) {
                    System.out.print(cell.getStringCellValue() + "\t\t");
                } else if (cell.getColumnIndex() == 1) {
                    System.out.print((long) cell.getNumericCellValue()+ "\t\t");
                } else if (cell.getColumnIndex() == 2) {
                    System.out.print((int) Math.round(cell.getNumericCellValue())+ "\t\t");


    } catch (FileNotFoundException fnfe) {
    } catch (IOException e) {
To run the above example, create a sheet in excel file with String, Long and Integer columns respectively.

To print column names, run a while loop with cell.getStringCellValue() on every cell in first row.

getStringNumericCellvalue() returns 'double' and you can see from the example that I've type-casted it to 'long' and 'int'.

To get a sheet by its index, use
in place of

Tuesday, November 4, 2014

Mac Yosemite : Ugly turned out to be Uglier and Ugliest

Mac Yosemite : Ugly turned out to be Uglier and Ugliest
You might have read my review on Mac OSX Yosemite, The Good, bad and ugly.
Now it turned out to be UGLIEST.

No more words. Here's the image.
Mac Yosemite : Ugly turned out to be Uglier and Ugliest

If you are on Yosemite, you might be familiar with it.
Most of the times, you get stuck on boot logo.
I've seen complaints regarding it saying that fellow members are ignorant of it and they deny such possibility, even though many are still facing it.

Workarounds suggested by our online friends:
  • Just reboot your mac as many times it takes to your desktop.
  • Boot into safe mode, by holding SHIFT and then reboot.
Comment your workaround below, mine is the first one.

If you are still on Mavericks, be there till Apple provides a fix for this.