Following the example on their site, http://jxls.sourceforge.net/getting_started.html. I came across a problem when trying to write the processed file. Instead of writing data that should be replaced by the JEXL, it simply just writes the template file again as if it were just copy and pasting the template file.
This is what my template file looks like:
And this is what it outputs:
My code the pretty much the same as the guide, so I'm not sure why my code is acting differently from theirs. There is one thing I changed that might affect it and that was using createInitialContext()
instead of getInitialContext()
. The reason for that was because for some reason my jar did not contain the getInitialContext()
method even though the jars are both the same (2.0.0), however, it did contain the createInitialContext()
method. From my debugging, it does seem like that the code processes the data and replace the JEXL with the proper data since the size of the xls area changes from 4 to 63.
My code:
InputStream is = getClass().getResourceAsStream("/transactions_template.xls");
OutputStream os = new FileOutputStream(file);
Transformer transformer = TransformerFactory.createTransformer(is, os);
AreaBuilder areaBuilder = new XlsCommentAreaBuilder(transformer);
List<Area> xlsAreaList = areaBuilder.build();
Area xlsArea = xlsAreaList.get(0);
Context context = transformer.createInitialContext();
context.putVar("transactions", transactions);
xlsArea.applyAt(new CellRef("Result!A1"), context);
transformer.write();
is.close();
os.close();
TLDR: JXLS seems to process the JEXL, but it writes the template file instead. I did change getInitialContext in the original guide to createInitialContext due to the createInitialContext not being on my version even though the guide uses my version, 2.0.0.
Have you checked the Result worksheet of the generated Excel document?
The example in the Getting Started guide does not change the template sheet and just generates all the output at Result sheet as indicated by CellRef parameter passed to applyAt method
xlsArea.applyAt(new CellRef("Result!A1"), context);
If you want to populate with the data the template sheet you should provide a proper CellRef to applyAt method. For example if your template sheet is named as Template you should have something like this
xlsArea.applyAt(new CellRef("Template!A1"), context);