Apache poi excel file throws confirmation before closing - java

I am using Apache poi for generating excel data.It works fine for all cell types except formula type cell.
The excel file with formula type cell when closed even without any changes throws a confirmatiorn like this.
For XLS fomat
http://i40.tinypic.com/126d9uw.jpg
For XLSX format:
http://i40.tinypic.com/126d9uw.jpg
Is there any way to suppress this confirmation.
I tried the below code but it didnt help.
//Workbook wb = WorkbookFactory.create(new FileInputStream(absolutexlsFileName));
FileInputStream fis = new FileInputStream(absolutexlsFileName);
Workbook wb = new XSSFWorkbook(fis);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateAll();
OutputStream out = msg.getHttpServletResponse().getOutputStream();
msg.getHttpServletResponse().setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
msg.getHttpServletResponse().setHeader("Expires:", "0"); // eliminates browser caching
msg.getHttpServletResponse().setHeader("Content-Disposition", "attachment; filename="+absolutexlsFileName);
wb.write(out);
out.flush();
out.close();

Looks like your formula changes would cause a cell update somewhere but you don't calculate it (but excel does and thus the change confirmation). You have to evaluate your formulas in order to prevent that warning [1]
[1] http://poi.apache.org/spreadsheet/eval.html

Related

Apache POI Clone Sheet corrupting the excel file

I have an excel template file I am retrieving from minIO, I perform some writing operation on this excel and I finally return the modified file to the user calling my code.
This flow was working fine until I tried to clone one of the sheet of my excel.
When I do that and I try to open it with Excel the file looks to be corrupted and Excel won't open it.
I am a bit lost, I am not sure what could cause this behaviour. I tried to clone an empty sheet I created for testing and it was working, so it might be related to the content of the sheet I am trying to clone but I have no idea as everything runs fine in the code.
So, theses are the part I consider relevants (getting the file from minIO, the moment I clone the sheet and finally when I write to a ByteArray)
try (InputStream inputStream = minioClient.getObject(
GetObjectArgs.builder()
.bucket("someBucket")
.object("someObject.xlsx")
.build()
)) {
Workbook workbook = new XSSFWorkbook(inputStream);
...
Sheet newSheet = workbook.cloneSheet(workbook.getSheetIndex(sheetTemplate));
workbook.setSheetName(workbook.getSheetIndex(newSheet.getSheetName()), "newName");
workbook.setSheetOrder("newName", workbook.getSheetIndex(sheetTemplate));
newSheet.getRow(10).getCell(2).setCellValue("someValue");
...
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
workbook.close();
return Optional.of(bos.toByteArray());
}
I know the question is pretty vague but maybe there is something in the way I handle this flow that it's incorrect although everything worked fine before I tried to clone some of the sheet of our template.
Any pointer would be highly appreciated.
Thank you in advance.

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

Converting HTML to proper Excel File

I currently create xls files by outputting an HTML table with the proper header and file extension. Here is the sample code:
String tabledata = "<table>MORE TABLE HTML HERE</table>";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"somefile.xls\"");
out.println(tabledata);
This works great except you get an error before opening "The file format and extension of 'somefile.xls' don't match." Once opened, it looks exactly how I want it.
I used Apache POI elsewhere, so figured I could try using this, but I don't want to have to build all elements in some long script looking for rows, columns and different styles. I was thinking that I could convert the HTML to an InputStream and then create the Workbook, but it doesn't like the headers. Here is the code:
InputStream is = new ByteArrayInputStream(tabledata.getBytes("UTF-8"));
HSSFWorkbook wb = new HSSFWorkbook(is);
FileOutputStream fileOut = new FileOutputStream("somefile.xls");
wb.write(fileOut);
fileOut.close();
Is something like this possible if the headers were added/changed? If so, how would I modify this code to work properly?

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