|
| 1 | +package com.baeldung.poi.excel.write.addimageincell; |
| 2 | + |
| 3 | +import java.io.FileOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | + |
| 7 | +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
| 8 | +import org.apache.poi.ss.usermodel.Row; |
| 9 | +import org.apache.poi.ss.usermodel.Sheet; |
| 10 | +import org.apache.poi.ss.usermodel.Workbook; |
| 11 | +import org.apache.poi.util.IOUtils; |
| 12 | +import org.apache.poi.xssf.usermodel.XSSFClientAnchor; |
| 13 | +import org.apache.poi.xssf.usermodel.XSSFDrawing; |
| 14 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 15 | + |
| 16 | +/** |
| 17 | + * This Helper class Add an Image to a Cell of an Excel File With apache-poi api. |
| 18 | + * |
| 19 | + */ |
| 20 | +public class ExcelCellImageHelper { |
| 21 | + |
| 22 | + public static void main(String[] args) throws IOException, InvalidFormatException { |
| 23 | + try (final Workbook workbook = new XSSFWorkbook(); |
| 24 | + FileOutputStream saveExcel = new FileOutputStream("target/baeldung-apachepoi.xlsx");) { |
| 25 | + |
| 26 | + Sheet sheet = workbook.createSheet("Avengers"); |
| 27 | + |
| 28 | + XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch(); |
| 29 | + XSSFClientAnchor ironManAnchor = new XSSFClientAnchor(); |
| 30 | + XSSFClientAnchor spiderManAnchor = new XSSFClientAnchor(); |
| 31 | + |
| 32 | + // Fill row1 data |
| 33 | + Row row1 = sheet.createRow(0); |
| 34 | + row1.setHeight((short) 1000); |
| 35 | + row1.createCell(0) |
| 36 | + .setCellValue("IRON-MAN"); |
| 37 | + updateCellWithImage(workbook, 1, drawing, ironManAnchor, "ironman.png"); |
| 38 | + |
| 39 | + // Fill row2 data |
| 40 | + Row row2 = sheet.createRow(1); |
| 41 | + row2.setHeight((short) 1000); |
| 42 | + row2.createCell(0) |
| 43 | + .setCellValue("SPIDER-MAN"); |
| 44 | + updateCellWithImage(workbook, 2, drawing, spiderManAnchor, "spiderman.png"); |
| 45 | + |
| 46 | + // Resize all columns to fit the content size |
| 47 | + for (int i = 0; i < 2; i++) { |
| 48 | + sheet.autoSizeColumn(i); |
| 49 | + } |
| 50 | + workbook.write(saveExcel); |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * This method position the anchor for a given rowNum and add the image correctly. |
| 57 | + * @param workbook |
| 58 | + * @param rowNum |
| 59 | + * @param drawing |
| 60 | + * @param inputImageAnchor |
| 61 | + * @throws IOException |
| 62 | + */ |
| 63 | + private static void updateCellWithImage(Workbook workbook, int rowNum, XSSFDrawing drawing, XSSFClientAnchor inputImageAnchor, String inputImageName) throws IOException { |
| 64 | + InputStream inputImageStream = ExcelCellImageHelper.class.getClassLoader() |
| 65 | + .getResourceAsStream(inputImageName); |
| 66 | + byte[] inputImageBytes = IOUtils.toByteArray(inputImageStream); |
| 67 | + int inputImagePictureID = workbook.addPicture(inputImageBytes, Workbook.PICTURE_TYPE_PNG); |
| 68 | + inputImageStream.close(); |
| 69 | + inputImageAnchor.setCol1(1); |
| 70 | + inputImageAnchor.setRow1(rowNum - 1); |
| 71 | + inputImageAnchor.setCol2(2); |
| 72 | + inputImageAnchor.setRow2(rowNum); |
| 73 | + drawing.createPicture(inputImageAnchor, inputImagePictureID); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments