Skip to content

Commit 709bd18

Browse files
sachin071287Sachin kumarashleyfrieze
authored
BAEL-5203 :Add an image to an Excel file with Java (eugenp#11391)
Co-authored-by: Sachin kumar <[email protected]> Co-authored-by: ashleyfrieze <[email protected]>
1 parent 85b913e commit 709bd18

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

apache-poi-2/pom.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
6-
<artifactId>apache-poi</artifactId>
6+
<artifactId>apache-poi-2</artifactId>
77
<version>0.0.1-SNAPSHOT</version>
8-
<name>apache-poi</name>
8+
<name>apache-poi-2</name>
99

1010
<parent>
1111
<groupId>com.baeldung</groupId>
@@ -17,8 +17,13 @@
1717
<dependency>
1818
<groupId>org.apache.poi</groupId>
1919
<artifactId>poi-ooxml</artifactId>
20-
<version>5.0.0</version>
20+
<version>${poi.version}</version>
2121
</dependency>
2222
</dependencies>
2323

24-
</project>
24+
<properties>
25+
<poi.version>5.0.0</poi.version>
26+
</properties>
27+
28+
</project>
29+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}
46.7 KB
Loading
55.7 KB
Loading

0 commit comments

Comments
 (0)