read.xlsx files using apache POI xssfworkbook ie XSSF api

Hi,

I am looking for good examples to read.xlsx files using apache POI xssfworkbook ie XSSF api.

I am getting following error when I try to read .xlsx file usiong HSSF api.
Excel file
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
      at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:131)
      at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
      at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
      at Processor2.read(Processor2.java:55)
      at  Processor2.main(Processor2.java:37)


Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
 
Amitkumar PConnect With a Mentor Sr. ConsultantCommented:
Check by adding poi-ooxml-3.8.jar in build path.
0
 
gudii9Author Commented:
I have poi-3.8.jar in my buildpath

I tried to read as .xlsx follows



public static Vector readXlsx(String fileName)    {
            Vector cellVectorHolder = new Vector();
            try{
                  FileInputStream myInput = new FileInputStream(fileName);
                  POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
                  XSSFWorkbook myWorkBook = new XSSFWorkbook(myFileSystem);
                  XSSFSheet mySheet = myWorkBook.getSheetAt(0);
                  Iterator rowIter = mySheet.rowIterator();
                  while(rowIter.hasNext()){
                        HSSFRow myRow = (HSSFRow) rowIter.next();
                        Iterator cellIter = myRow.cellIterator();
                        Vector cellStoreVector=new Vector();
                        while(cellIter.hasNext()){
                              HSSFCell myCell = (HSSFCell) cellIter.next();
                              cellStoreVector.addElement(myCell);
                        }
                        cellVectorHolder.addElement(cellStoreVector);
                  }
            }catch (Exception e){e.printStackTrace(); }
            return cellVectorHolder;
      }

I am getting compilation errors as my program not able to read XSSFWorkbook , XSSFSheet for some reason.

I tried to import as well as below

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;



My program is not recognizing these imports by giving compilation error. Please advise
0
 
gudii9Author Commented:
sure
0
 
gudii9Author Commented:
now all compilation errors are gone. When I run java application seeing error as

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

Please advise
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.