Search code examples
javaexcelimage-processingjxls

jXLS jx:image ends with java.lang.IllegalArgumentException: imgBean value must contain image bytes


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?!


Solution

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

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