dimanche 5 février 2023

Custom position for image after merge cell in excel by java

[Java] After merge 4 or 5 rows of second column in excel from the second row, I inserted a image to this area by anchor support. Finally, I wish this image is center aligned auto in merged cell. I hope every one can support to me!

XSSFWorkbook workbook = null;//you can insert
XSSFSheet sheet = null;//you can insert
Pattern p = Pattern.compile("src=\"(.*?)\"");
        Matcher matcher = p.matcher(htmlThumbail);
        if(matcher.find()) {
            String[] base64Image = matcher.group(1).split(",");
            int picFormat;
            switch (base64Image[0]) {//check image's extension
                case "data:image/jpeg;base64":
                    picFormat = workbook.PICTURE_TYPE_JPEG;
                    break;
                case "data:image/png;base64":
                    picFormat = workbook.PICTURE_TYPE_PNG;
                    break;
                default:
                    picFormat = workbook.PICTURE_TYPE_JPEG;
                    break;
            }
            int pictureIdx = workbook.addPicture(Base64.getDecoder().decode(base64Image[1]),  picFormat);
            XSSFClientAnchor anchor = new XSSFClientAnchor();
            anchor.setCol1(2);
            anchor.setRow1(subRow);
            anchor.setCol2(3);
            anchor.setRow2(subRow + 1);
            anchor.setDx1(5 * Units.EMU_PER_PIXEL);
            anchor.setDy1(5 * Units.EMU_PER_PIXEL);
            anchor.setDx2(-5 * Units.EMU_PER_PIXEL);
            anchor.setDy2(-5 * Units.EMU_PER_PIXEL);
            XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
            drawing.createPicture(anchor, pictureIdx);

Demo by capture

XSSFWorkbook workbook = null;//you can insert
XSSFSheet sheet = null;//you can insert
Pattern p = Pattern.compile("src=\"(.*?)\"");
        Matcher matcher = p.matcher(htmlThumbail);
        if(matcher.find()) {
            String[] base64Image = matcher.group(1).split(",");
            int picFormat;
            switch (base64Image[0]) {//check image's extension
                case "data:image/jpeg;base64":
                    picFormat = workbook.PICTURE_TYPE_JPEG;
                    break;
                case "data:image/png;base64":
                    picFormat = workbook.PICTURE_TYPE_PNG;
                    break;
                default:
                    picFormat = workbook.PICTURE_TYPE_JPEG;
                    break;
            }
            int pictureIdx = workbook.addPicture(Base64.getDecoder().decode(base64Image[1]),  picFormat);
            XSSFClientAnchor anchor = new XSSFClientAnchor();
            anchor.setCol1(2);
            anchor.setRow1(subRow);
            anchor.setCol2(3);
            anchor.setRow2(subRow + 1);
            anchor.setDx1(5 * Units.EMU_PER_PIXEL);
            anchor.setDy1(5 * Units.EMU_PER_PIXEL);
            anchor.setDx2(-5 * Units.EMU_PER_PIXEL);
            anchor.setDy2(-5 * Units.EMU_PER_PIXEL);
            XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
            drawing.createPicture(anchor, pictureIdx);

Aucun commentaire:

Enregistrer un commentaire