org.apache.poi.poifs.filesystem.OfficeXmlFileException - java

i want to read a microsoft word document i.e. .docx format using POI but getting error:
The supplied data appears to be in the Office 2007+ XML. POI only supports OLE2 Office documents
if anyone can help me to get rid of this??

You should have a read through the Converting from HSSF to the Common SS Usermodel page to help you understand what you need to change.
As a general guide though, if your code was previously
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("foo.xls"));
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = s.getRow(0);
System.out.println("Cell A1 is " + r.getCell(0));
It should instead become
Workbook wb = WorkbookFactory.create(new File("foo.xls")); // Or foo.xlsx
Sheet s = wb.getSheetAt(0);
Row r = s.getRow(0);
System.out.println("Cell A1 is " + r.getCell(0));

Please check http://poi.apache.org/spreadsheet/converting.html about new models XSSF and HSSF.

Not sure which API from POI you are using, but I presume that you are using the HSSF API. Instead you should use the the XSSF API (see http://poi.apache.org/spreadsheet/index.html for details). For example instead of using Workbook wb = new HSSFWorkbook(); use Workbook wb = new XSSFWorkbook();

HSSF workbook will not work for higher version of excel file instead of HSSFWorkbook use XXSFWOrkbook.You can have the complete code in the below link
http://aravind-soa.blogspot.com/2017/02/how-to-import-excel-file-into-oracle.html

Related

How to create pivot table in SXSSF sheet using apache poi library in java?

XSSFSheet sheet = workbook.getXSSFWorkbook().getSheetAt(0);
XSSFSheet pivot_sheet = workbook.getXSSFWorkbook().createSheet("Sheet1");
XSSFPivotTable pivotTable = pivot_sheet.createPivotTable(new AreaReference("B10:AJ24"), new CellReference("B2"),sheet);
I am getting following error at above line -
java.lang.NullPointerException at
org.apache.poi.xssf.usermodel.XSSFPivotCacheDefinition.createCacheFields(XSSFPivotCacheDefinition.java:145)
at
org.apache.poi.xssf.usermodel.XSSFSheet.createPivotTable(XSSFSheet.java:4065)
I didn't find solution for this using apache poi.
Workaround that I used is mentioned as follows :
Create SXSSFWorkbook as below
SXSSFWorkbook workbook = new SXSSFWorkbook(new XSSFWorkbook(new FileInputStream("xyz.xlsm")));
workbook.removeSheetAt(0);
xyz.xlsm is macro enabled excel which holds VBA code for generating pivot
Write in workbook as usual
Pivots will get generated in excel on opening it after download

Error: Type mismatch: cannot convert from HSSFWorkbook to Workbook

I use poi-3.2-FINAL-20081019.jar . Error:
Type mismatch: cannot convert from HSSFWorkbook to Workbook
try {
if (strType.equals("xls")) {
wb = new HSSFWorkbook(inputStream);
} else {
wb = new XSSFWorkbook(inputStream);
}
Sheet sheet = wb.getSheetAt(0);
How to fix it?
As the date in your jar shows - poi-3.2-FINAL-20081019.jar - you're usigng a jar that's almost 10 years old! You need to upgrade to something more modern, and at the very least something from this decade...
Right now (November 2017), the latest version is Apache POI 3.17. You can find the latest version on the Apache POI homepage, and see all the fixes in the Changelog
In addition, you should swap to using WorkbookFactory rather than looking at file extensions to work out what class to use. That hides all the detection complexity for you, works around mis-named files etc
Your code can then become the very simple
Workbook wb = WorkbookFactory.create(new File("input.xlsx"));
Sheet s = wb.getSheetAt(0);
(Use a File if you can, rather than InputStream, for lower memory)
Use newer version:
https://mvnrepository.com/artifact/org.apache.poi/poi/3.5-FINAL
Read more at: http://poi.apache.org/spreadsheet/converting.html

java apache syntax poi 3.11 vs 3.16

I have developed a program in which POI 3.11 is used. After upgrading to POI 3.16, the error ..depricated .. is shown for some instances (for example, HSSFCellStyle.BORDER_THIN).
Despite intensive search, I am unable to find the correct new syntax for the function ...setBorderLeft(HSSFCellStyle.BORDER_THIN).
Here is the section of the code:
try {FileInputStream file = new FileInputStream(new File(eP));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFFont cF = workbook.createFont();
HSSFCellStyle cS=workbook.createCellStyle();
cS.setBorderLeft(HSSFCellStyle.BORDER_THIN);
How can I migrate the code to POI 3.16?
Greetings from Aachen
According to the documentation of Apache Poi, since the version 3.15, the method setBorderLeft(....) consumes newly the instance of the BorderStyle enum.
I suggest you to try the following:
HSSFCellStyle cS=workbook.createCellStyle();
cS.setBorderLeft(BorderStyle.THIN);

How to distinguish between XSSF and HSSF automatically in Apache POI?

I would like to be able to open Excel file of arbotrary type. Is it possible to select between HSSFWorkbook and XSSFWorkbook automatically?
Currently I write
Workbook workbook = new HSSFWorkbook(excelFile);
Can I write universal?
Yes! All you need to do is use WorkbookFactory
As per this part of the docs, it's better to use a File than an InputStream. So, just do something like:
File file = new File("input.xls");
Workbook wb = WorkbookFactory.create(file);
That will create whichever of HSSFWorkbook or XSSFWorkbook your file needs

Java poi convert xml to xls

I have file in type xls with xml extension, I want to read it like excel file.
I trying
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(inputFile));
HSSFSheet sheet = wb.getSheetAt(0);
but it read only with xls extension.
someone know how??
thank
You have to use the XSSF* Classes.
XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.
https://poi.apache.org/spreadsheet/index.html
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));
XSSFSheet sheet = wb.getSheetAt(0);

Categories