I've got an table in excel with formulae I would like to add data to.
My motivation for this is the fact that tables in excel can dynamically expand to the range of data you add to them, meaning that the formula rows automatically keep up with the amount of data rows.
I'm however having a hard time finding out if this is possible using apache-POI.
One thing I was going to try (see code below) was to expand the AreaReference
of the table to cover the data, however both AreaReference(CR,CR2);
(as used in this example) and AreaReference(CR,CR2, SpreadsheetVersion.EXCEL2007)
(seen in the apache docs) give "constructor is undefined".
No idea what is causing that constructor error as I do have org.apache.poi.ss.util
imported.
The other option on the apache docs AreaReference(java.lang.String reference)
lets me compile and run but instead gives a "NoSuchMethod" error.
List<XSSFTable> tableList = spreadSheet.getTables();
CellReference CR = new CellReference(0, 0);
CellReference CR2 = new CellReference(5, 2);
AreaReference my_data_range = new AreaReference(CR,CR2);
tableList.get(0).setArea(my_data_range);
Any help will be appreciated.
The main problem using
apache poi
until now is that it is not ready to be used without having detailed knowledge aboutMicrosoft Office
as such and about the storage ofMicrosoft Office
files. There are many things only half way ready and there are regressions often in new versions (bugs occur again which were solved already).So your requirement: "Expanding an existing table in Excel using Apache POI" is not possible only simply using
apache poi
. One must know thatOffice Open XML
files*.xlsx
are simplyZIP
archives which can be unzipped. And after unzipping we find/xl/tables/table1.xml
for storage of the table. ThisXML
we can analyzing and comparing it withXML
which was created usingExcel
'sGUI
. So we can find problems which results from shortcomings ofapache poi
. Same is with the sheet'sXML
in/xl/tables/sheet1.xml
.Also we need to know that
apache poi
builds on the low level classes ofooxml-schemas
. Partially we need using those classes because of the halfway readiness ofapache poi
. In the following example we needooxml-schemas-1.4.jar
additionally becauseapache poi
'spoi-ooxml-schemas-4.0.0.jar
has not includedorg.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableFormula
until now. Unfortunately there is no documentation aboutooxml-schemas
public available. So we need downloading the sources and doingjavadoc
our own.The following example works for me using
apache poi 4.0.0
. If you get problems while compiling or running, the reason might be that multiple different versions ofapache poi
jar
s are in class path while compile time and/or run time. Do not mix different apache poi versions. Also, as said already, my code needs the full jar of all of the schemas ooxml-schemas-1.4.jar.