2

I want to put an image in XLS file template with jXLS 2.2.3

XLS template is defined as follows:

Cell A1, added comment: jx:area(lastCell="L10")

Cell A10, added comment: jx:each(items="rows" var="r" lastCell="L10")

Cell D10, add comment: jx:image(imgBean="r.barcodeJpgImage" imageType="JPEG" lastCell="L10")

When I execute the code:

        ByteArrayOutputStream fos = new ByteArrayOutputStream();
        //fis is template file input stream, fos is the output
        Transformer transformer = TransformerFactory.createTransformer(fis, fos);
        AreaBuilder areaBuilder = new XlsCommentAreaBuilder(transformer);
        List<Area> xlsAreaList = areaBuilder.build();
        Area xlsArea = xlsAreaList.get(0);
        Context context = new Context();
        for (Map.Entry<String, Object> entry : beans.entrySet()) {
            context.putVar(entry.getKey(), entry.getValue());
        }
        xlsArea.applyAt(new CellRef(0,0), context);
        transformer.write();

I get this exception:

Caused by: java.lang.IllegalArgumentException: imgBean value must contain image bytes (byte[]) at org.jxls.command.ImageCommand.applyAt(ImageCommand.java:76) at org.jxls.area.XlsArea.applyAt(XlsArea.java:142) at org.jxls.command.EachCommand.applyAt(EachCommand.java:177) at org.jxls.area.XlsArea.applyAt(XlsArea.java:142)

Variable r.barcodeJpgImage is not null and contains JPEG image bytes.

Any idea what might be wrong?!

Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48
  • The Image-command in jxls-2 up to v2.2.4 does not support evaluation of image from a bean property. Probably this support will be added in the upcoming v2.2.5 release. – Leonid Vysochyn Sep 04 '15 at 16:18

1 Answers1

1
  1. Upgrade to jxls-2.2.5 or later
  2. Specify Image-command like this

    jx:image(src="r.barcodeJpgImage" imageType="JPEG" lastCell="L10")

Leonid Vysochyn
  • 1,254
  • 1
  • 7
  • 12
  • Thank you Leonid! This solves the problem that I described, so I mark it as a correct answer. However, there is another one - I don't see the picture in the XLS file. No errors are thrown. The byte array is passed to the lib. The picture is quite big, compared to the size of the cell in the template. Could that be a problem?! – Georgi Stoyanov Sep 07 '15 at 07:25
  • It is difficult to say what could be the issue. Try to adjust ImageDemo example in jxls-demo project to use your picture and see if it works for you. – Leonid Vysochyn Sep 07 '15 at 08:21
  • I could't find that demo on the web site. Though I spent some time in debugging. For my test case that I described in the question, I have only 1 row. I changed image format to PNG and lastCell is D10 The problem I found is in org.jxls.transform.jexcel.JexcelTransformer, line 191: sheet.addImage(new WritableImage(areaRef.getFirstCellRef().getCol(),areaRef.getFirstCellRef().getRow(), areaRef.getLastCellRef().getCol() - areaRef.getFirstCellRef().getCol(), areaRef.getLastCellRef().getRow() - areaRef.getFirstCellRef().getRow(),imageBytes)); Params width and height are 0. – Georgi Stoyanov Sep 07 '15 at 09:42
  • when I change those values to 1, in the debugger, the image is added successfully. So, I assume there is a problem in the areas calculations when jx:image is used within jx:each command?! – Georgi Stoyanov Sep 07 '15 at 09:47
  • The Image demo can be found in https://bitbucket.org/leonate/jxls-demo or by downloading the full jxls distribution from sourceforge site. – Leonid Vysochyn Sep 08 '15 at 09:01