1

I am running below my code in STS with java 1.7 version on ubuntu 14 version and I am unable solve the issue even though I have tested below 2 programs by adding all the jars of Apache poi 3.9 version once and with version 3.2 for second time. but every time it is giving almost same error
Kindly suggest me some thing Thanks in advance.

Following is my code to read data from .xlsx file

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadData {

    public static void main(String a[]) throws FileNotFoundException {

        try {
            File file = new File("Test.xlsx");
            FileInputStream fis = new FileInputStream(file);
            XSSFWorkbook hwb = new XSSFWorkbook(fis);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}

and error I am getting is

Exception in thread "main" org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39) at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:273) at com.axxera.selenium.ReadData.main(ReadData.java:18) Caused by: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:201) at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:684) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:275) at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)

And below code for reading data from .xls file

public class ReadData {

    public static void main(String a[]) throws FileNotFoundException {

        try {
            File file = new File("Test.xls");
            FileInputStream fis = new FileInputStream(file);
            HSSFWorkbook hwb = new HSSFWorkbook(fis);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}

and above code is generating this error

Exception in thread "main" 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:128) at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:112) at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:300) at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:400) at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:381) at com.axxera.selenium.ReadData.main(ReadData.java:17)

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
VenkatKrishna
  • 127
  • 3
  • 11
  • Did you import all necessary JARs of POI? – Helping Hands Dec 15 '15 at 10:18
  • Can you list out which JARs you have imported? – Helping Hands Dec 15 '15 at 10:19
  • take a look at this question.. may be this helps http://stackoverflow.com/questions/21992071/org-apache-poi-poixmlexception-org-apache-poi-openxml4j-exceptions-invalidforma – awsome Dec 15 '15 at 10:20
  • /home/axxera/Jars/ApachePoi/poi-3.13/poi-scratchpad-3.13-20150929.jar /home/axxera/Jars/ApachePoi/poi-3.13/poi-ooxml-schemas-3.13-20150929.jar /home/axxera/Jars/ApachePoi/poi-3.13/poi-ooxml-3.13-20150929.jar /home/axxera/Jars/ApachePoi/poi-3.13/poi-excelant-3.13-20150929.jar /home/axxera/Jars/ApachePoi/poi-3.13/poi-examples-3.13-20150929.jar /home/axxera/Jars/ApachePoi/poi-3.13/poi-3.13-20150929.jar /home/axxera/Jars/ApachePoi/poi-3.13/lib/log4j-1.2.17.jar – VenkatKrishna Dec 15 '15 at 10:22
  • /home/axxera/Jars/ApachePoi/poi-3.13/lib/junit-4.12.jar /home/axxera/Jars/ApachePoi/poi-3.13/lib/commons-logging-1.1.3.jar /home/axxera/Jars/ApachePoi/poi-3.13/lib/commons-codec-1.9.jar /home/axxera/Jars/ApachePoi/poi-3.13/ooxml-lib/xmlbeans-2.6.0.jar – VenkatKrishna Dec 15 '15 at 10:22
  • above are 3.13 jars and also i have tested with 3.9 FINAL jar – VenkatKrishna Dec 15 '15 at 10:22
  • HI @awsome it is giving following error org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:201) at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:684) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:275) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:209) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:168) at com.axxera.selenium.ReadData.main(ReadData.java:21) – VenkatKrishna Dec 15 '15 at 10:30

3 Answers3

2

Seems like you saved xls under xlsx and visa versa.

Try Workbook wb = WorkbookFactory.create(file | inputStream). It opens file independent of the file extension.

dlopatin
  • 3,752
  • 1
  • 17
  • 18
1

Your code is working fine with poi-3.5-FINAL.jar and with .xls file

Sonal
  • 262
  • 5
  • 22
0

you'r code in OK for .xls . but you should handle the another file extension (.xlsx)

amir
  • 2,443
  • 3
  • 22
  • 49