2

I need to know if those libraries mentioned in the title are compatible between each other. What I need to do is to change libraries: from net.sf.jxls to org.jxls 2.10.0 (and therefore adapt the implementation that was made using jxls-core 1.0.6). I'm working with java 8.

According to the implementation that I need to adapt, jxls is used by first instanciating and XLSTransformer object: XLSTransformer transformer = new XLSTransformer(); Then, the method 'transformXLS(...)' is called which receives an InputStream as parameter and a Map, and returns a Workbook object.

Is there a similar method or some kind of 'work-around' in jxls 2.10.0 to perform exactly the same? What I need to know is a way to return a Workbook object using jxls 2.10.0 in order to adapt the implementation done with jxls-core 1.0.6

Paul
  • 21
  • 3
  • 1
    I don't believe that there is something similar in current `JXLS`. Why you cannot simply use the preferred way using `JxlsHelper` as described in http://jxls.sourceforge.net/samples/object_collection.html? Hint: `OutputStream os` might be a `ByteArrayOutputStream` too. – Axel Richter Apr 09 '21 at 05:53
  • Thanks for the answer! I've tried that implementation but I need to return an array of bytes, is there some way to get a byte[] from ByteArrayOutputStream after processing the template? – Paul Apr 11 '21 at 19:42
  • [ByteArrayOutputStream.toByteArray](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toByteArray--)? – Axel Richter Apr 12 '21 at 03:49

2 Answers2

0

While jxls1 does not directly support POI4, it is easy to modify it for this purpose.

You basically have to edit these classes (in jxls-core):

  • net/sf/jxls/parser/Cell.java
  • net/sf/jxls/parser/CellParser.java
  • net/sf/jxls/transformer/CellTransformer.java
  • net/sf/jxls/transformer/XLSTransformer.java
  • net/sf/jxls/util/TagBodyHelper.java
  • net/sf/jxls/util/Util.java

You need minor changes on them, e.g.:

XLSTransformer.java: line 484, change

if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) {

to

if (cell != null && cell.getCellType() == CellType.STRING) {

so basically all changes are minor changes. You can find the jxls1 code with POI 4 support here: https://github.com/infofabrik/reportserver/tree/main/jxls-src

We are also working on sending this code to the jxls team. But you can of course use the classes available in the link.

Edu
  • 160
  • 10
0

I also recently upgraded from 1.x to 2.x of JXLS and ran into this issue. I used this code to take an input stream of an Excel file, then process the template with a Map of <String, Object> entries, and finally retrieve the workbook.

PoiTransformer transformer = PoiTransformer.createTransformer(is);
JxlsHelper.getInstance().processTemplate(new Context(yourStringObjectMap), transformer);
Workbook workbook = transformer.getWorkbook();
Todd
  • 1,822
  • 15
  • 18