@ankur8819 • 02 May, 2012 • 2 likes
Hi CEans ,
Was doing a POC for reading a Spreadsheet in .xls and .xlsx format using Apache POI.
Thought might be useful to you guys.
The code is the most basic version of Reading from either of the formats.Its applications are endless.let me know if there is some specific requirement.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class ExcelRead {
    public static void main(String ar[]) {
        String fname = "B:\\AnkurTostudy\\poi-3.8\\TestSheet.xlsx";
        Workbook workbook = null;
        Sheet sheet = null;
        try {
            InputStream in = new FileInputStream(fname);
            try {
                workbook = WorkbookFactory.create(in);
                sheet = workbook.getSheetAt(0);
                int noOfRows = sheet.getPhysicalNumberOfRows();
                for (int rownum = 0; rownum < noOfRows; rownum++) {
                    Row row = sheet.getRow(rownum);
                    int noOfColumns = row.getLastCellNum();
                    for (int colnum = 0; colnum < noOfColumns; colnum++) {
                        Cell cell = row.getCell(colnum);
                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                            } else {
                        case Cell.CELL_TYPE_BOOLEAN:
                        case Cell.CELL_TYPE_FORMULA:
                            System.out.println("In Default");
            } catch (InvalidFormatException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
Here is how the Build Path looks like.
@Ankita Katdare • 03 May, 2012 Thanks for the share ankur8819 😀 I am sure it will be helpful to CEans.
@PraveenKumar Purushothaman • 06 May, 2012 Awesome... Let me extract the information about Apache POI for the others:
The Apache POI Project's mission is to create and maintain Java APIs for manipulating various file formats based upon the Office Open XML standards (OOXML) and Microsoft's OLE 2 Compound Document format (OLE2). In short, you can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java. Apache POI is your Java Excel solution (for Excel 97-2008). We have a complete API for porting other OOXML and OLE2 formats and welcome others to participate.

OLE2 files include most Microsoft Office files such as XLS, DOC, and PPT as well as MFC serialization API based file formats. The project provides APIs for the OLE2 Filesystem (POIFS) and OLE2 Document Properties (HPSF).

Office OpenXML Format is the new standards based XML file format found in Microsoft Office 2007 and 2008. This includes XLSX, DOCX and PPTX. The project provides a low level API to support the Open Packaging Conventions using openxml4j.

For each MS Office application there exists a component module that attempts to provide a common high level Java api to both OLE2 and OOXML document formats. This is most developed for Excel workbooks (SS=HSSF+XSSF). Work is progressing for Word documents (HWPF+XWPF) and PowerPoint presentations (HSLF+XSLF).

The project has recently added support for Outlook (HSMF). Microsoft opened the specifications to this format in October 2007. We would welcome contributions.

There are also projects for Visio (HDGF), TNEF (HMEF), and Publisher (HPBF).

As a general policy we collaborate as much as possible with other projects to provide this functionality. Examples include: Cocoon for which there are serializers for HSSF; Open Office.org with whom we collaborate in documenting the XLS format; and Tika / Lucene, for which we provide format interpretors. When practical, we donate components directly to those projects for POI-enabling them.

Related Posts

I thought of compiling a list of the coolest and the best whatsapp group names that you've been using for your respective groups. For the sake of convenience, we'll stick...
A simple good bye email or last working day mail ain't cool! We've some awesome templates that you can use. If you're looking for an amazing Last Working Day email...
Hello CEans, How to convert CGPA (10 Point Scale) to Percentage? Please help!
I heard many students are having a trouble registering on the TCS Portal, when it's quite straightforward actually. If you have problem in attaching photos or CV, Use internet explorer...
What is IES or Indian Engineering Services Exam? Union Public Services Commission (UPSC) conducts the Engineering Services Exam or ESE 2016 is an entrance exam for graduates who wish to...