Read Excel file in Java using POI API


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;
public class ReadExcelFileUsingPOI{
    public static void main(String[] args) throws Exception {
        String filename = "D:\\test.xls";
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filename);
            HSSFWorkbook workbook = new HSSFWorkbook(fis);
            HSSFSheet sheet = workbook.getSheetAt(0);
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator cells = row.cellIterator();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();
                    int type = cell.getCellType();
                    if (type == HSSFCell.CELL_TYPE_STRING) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                                + cell.getColumnIndex() + "] = STRING; Value = "
                                + cell.getRichStringCellValue().toString());
                    else if (type == HSSFCell.CELL_TYPE_NUMERIC) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                                + cell.getColumnIndex() + "] = NUMERIC; Value = "
                                + cell.getNumericCellValue());
                    else if (type == HSSFCell.CELL_TYPE_BOOLEAN) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                                + cell.getColumnIndex() + "] = BOOLEAN; Value = "
                                + cell.getBooleanCellValue());
                    else if (type == HSSFCell.CELL_TYPE_BLANK) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                                + cell.getColumnIndex() + "] = BLANK CELL");
                    }
                }
            }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        finally {
            if (fis != null) {
                fis.close();
            }
        }
    }
}
Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , , , , ,
Posted in JAVA, POI API
2 comments on “Read Excel file in Java using POI API
  1. Akram says:

    Why dont you try Fillo? It is a freeware product and used to query Excel files (xls & xlsx). Now, it supports SELECT and UPDATE queries with or without WHERE clause.

    http://www.codoid.com/products/view/2/29

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 678 other followers

Calendar
September 2012
M T W T F S S
« Jul   Oct »
 12
3456789
10111213141516
17181920212223
24252627282930
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 262,785 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100