VenkatKrishna
VenkatKrishna

Reputation: 127

Reading contents from .xls or .xlsx file using Java and Apache poi

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)

Upvotes: 1

Views: 2794

Answers (3)

amir
amir

Reputation: 2573

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

Upvotes: 0

Sonal
Sonal

Reputation: 272

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

Upvotes: 1

dlopatin
dlopatin

Reputation: 3812

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.

Upvotes: 2

Related Questions