[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5869
  • Last Modified:

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
0
gudii9
Asked:
gudii9
  • 3
1 Solution
 
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
 
Amitkumar PSr. ConsultantCommented:
Check by adding poi-ooxml-3.8.jar in build path.
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now