Java poi convert xml to xls - java

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);

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

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

.getAllPictures() Method doesn't get all the pictures in .xlsx excel file

I can't get all the pictures in .xlsx file that I made in MS Excel while when I used Google spreadsheet to create a .xlsx file it can read all the pictures that I inserted there.
My code is here:
XSSFWorkbook workbook = new XSSFWorkbook("Sample.xlsx");
XSSFSheet sheet = workbook.getSheetAt(0);
List lst = workbook.getAllPictures();
Is there a way on how to read ALL the images in a .xlsx file which is done by MS Excel?

error while inputing an csv file into java code

the question is how can i use java to input a csv file and output an xls file
is there any specefic api or lib which can help question gonna help so why it s error on opening a sheet
FileOutputStream fos = new FileOutputStream(outputFile);
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
HSSFSheet sheet = workbook.getSheetAt(0);
after i tried i think it s due to a memor problem idk

org.apache.poi.poifs.filesystem.OfficeXmlFileException

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

Categories