Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Am P
Am P
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

sure
Avatar of gudii9

ASKER

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