user1732091
user1732091

Reputation: 41

jxls - create Excel file with dynamic images

I want to create an Excel file with a dynamic logo and a list of dynamic images. The creation of the text part in the Excel file works great with jxls. But I'm not able to insert one image in each row with jxls.

Please, does anyone have advice for me?

Thank you in advance.

Upvotes: 3

Views: 3830

Answers (2)

omar.zhou
omar.zhou

Reputation: 23

I have achieved this problem.Below are the hints: jx:each(items="persons" var="person" lastCell="G2" direction="DOWN")

jx:image(lastCell="F2" src="person.portrait")

hope it works for you.

Upvotes: 0

user1732091
user1732091

Reputation: 41

The only solution I found was to create the XLS file with JXLS and after creating the workbook insert the images with native POI functions like :

Workbook resultWorkbook = transformer.transformXLS(is, beans);

        Iterator rowIter = resultWorkbook.getSheetAt(0).rowIterator();
        while (rowIter.hasNext()){
            HSSFRow row = (HSSFRow) rowIter.next();
            Iterator cellIter = row.cellIterator();
            while (cellIter.hasNext()){
                HSSFCell cell = (HSSFCell) cellIter.next();
                final String IMG_PREFIX = "#IMG#";
                if(cell.toString().startsWith(IMG_PREFIX)){
                    String cellValue = cell.toString();
                    cell.setCellValue("");
                    String imagePath = cellValue.replaceFirst(IMG_PREFIX, "");
                    File imageFile = new File(imagePath);
                    if(imageFile.exists()){
                        FileInputStream isi = new FileInputStream(imageFile);
                        ImageTools imageTools = new ImageTools();
                        byte[] imgBytes = imageTools.resizeImage(isi, 100);
                        int pictureIdx = resultWorkbook.addPicture(imgBytes, Workbook.PICTURE_TYPE_JPEG);
                        CreationHelper helper = resultWorkbook.getCreationHelper();

                        Drawing drawing = resultWorkbook.getSheetAt(0).createDrawingPatriarch();
                        ClientAnchor anchor = helper.createClientAnchor();
                        anchor.setCol1(cell.getColumnIndex());
                        anchor.setRow1(cell.getRowIndex());
                        Picture pict = drawing.createPicture(anchor, pictureIdx);
                        pict.resize();
                        isi.close();
                    }
                }
            }
        }

Upvotes: 1

Related Questions