A
A
Alecxandrys2015-05-20 18:08:02
Java
Alecxandrys, 2015-05-20 18:08:02

Why does formatting break when editing documents using POI?

Good afternoon
When forming a document in xls by means of POI, the formatting and macros of a copy of the original document are lost, which is processed. How to preserve the formatting of the original document when pouring your data into it and saving it?
The source code of the block that forms the document.
The file variable is a link to the original document

fileInputStream = new FileInputStream(file);

Workbook wb = WorkbookFactory.create(fileInputStream); 
fileOutputStream = new FileOutputStream(file.getAbsolutePath().substring(0,
file.getAbsolutePath().length()-4)+"_returned.xls");
wb.write(fileOutputStream);
fileOutputStream.close();
fileInputStream.close();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alecxandrys, 2015-05-25
@Alecxandrys

The problem was solved by saving the source file as xlsm (xlsx with macros) and a new document was formed from a template of this format. All macros and formatting are preserved in the new document.

V
Vladimir Smirnov, 2015-05-22
@bobzer

Yes, your file also loses its formatting after copying from POI, and generally opens with errors. As I expected, the problem is in the file itself: there is too much stuff in it, even password protection for changes... Unfortunately, POI is not omnipotent, and it does not support all Excel functions. And it's not so much the POI developers, but the format used by Microsoft. POI developers even use the abbreviation HSSF - Horrible Spreadsheet Format (horrible spreadsheet format). I think that in your case there is no solution to the problem with POI in its pure form, you need to look for a workaround. You can try to use the native Microsoft API (difficult for Java), or somehow change the source file, or put up with losses and take only data from the file (and implement formatting in your code).
In general, POI is mainly used to upload data from Java programs, and working with arbitrary existing files is rarely used ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question