Images make you understand any story the easy way. It’s a famous saying that a picture is worth a thousand words. Microsoft Excel lets you insert pictures in your worksheets in cells and recording facts against these. In this article, we’ll show how to use Apache POI API to insert an image in Excel XLSX file using Java.

Apache POI

Apache POI is a Java library for working with Microsoft Office file formats such as XLS, XLSX, DOC, DOCX, PPT, PPTX, etc. Java application developers can use it for document processing without getting into the underlying details of the internal file structure of these files.

Installing Apache POI API

Before we can start with writing code for working with Excel documents in our Java application, we need to install Apache POI on our system. For this purpose, we should have the POI dependencies available in our Project setup. The following dependencies are required to develop the Apache POI application.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

You can find more information about installing the POI API for working with Excel files. Once added to your project, the pom.xml file looks as follows.

How to Insert Image in Excel Cell using Java?

You can use the Apache POI library to open an Excel workbook in Java and insert images in an Excel cell using the following steps.

Steps to add an image in XLSX using Java POI

  1. Create an instance of XSSFWorkbook class
  2. Create a Row and add Cells in the row
  3. Load the image as an InputStream object
  4. Convert the input stream to a byte array
  5. Add the picture to the workbook from the byte array using the addPicture method of the Workbook instance

Add Image in Excel using Java POI Example

Let’s look at the complete sample code to add an image in an Excel cell using the Java POI library.

//create a new workbook
XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
//add picture data to this workbook.
FileInputStream is = new FileInputStream("FileFormat.png");
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
is.close();
XSSFCreationHelper helper = wb.getCreationHelper();
//create sheet
XSSFSheet sheet = wb.createSheet();
// Create the drawing patriarch.  This is the top level container for all shapes.
Drawing drawing = sheet.createDrawingPatriarch();
//add a picture shape
XSSFClientAnchor anchor = helper.createClientAnchor();
//set top-left corner of the picture,
//subsequent call of Picture#resize() will operate relative to it
anchor.setCol1(3);
anchor.setRow1(2);
Picture pict = drawing.createPicture(anchor, pictureIdx);
//auto-size picture relative to its top-left corner
pict.resize();
//save workbook
String file = "picture.xls";
if(wb instanceof XSSFWorkbook) file += "x";
try (FileOutputStream fileOut = new FileOutputStream(file)) {
    wb.write(fileOut);
    wb.close();
}
catch(Exception ex)
{
	System.out.println(ex.getMessage());
}

Conclusion

In this article, we have shown how to add an image to an Excel XSLX workbook using Apache POI library for Java. We’ll be adding more Apache POI examples for working with Excel files using Apache POI API. So stay tuned.