XLSX - Insert PNG / JPG Picture- Java POI Example Program

XLSX File - Insert Image - Introduction


In the last article, we introduced you to inserting images in XLS documents, using Apache POI library and Java. We did not cover XLSX version in that tutorial as XLSX uses different set of class files. It is not always mandatory to have one XLS and one XLSX version. You can have a generic code covering both. In this tutorial, we will cover how to add pictures to XLSX workbooks, using Apache POI. The steps would be exactly on the same lines as of the last post, so, you can refer to that one as a base.

For a change we will add a PNG Image (A logo, for instance) to the Excel workbook. The sample PNG Image that we want to add is shown below:


XLSX - Add PNG Image to Workbook - Java POI Example - Input Image
XLSX - Add PNG Image to Workbook - Java POI Example - Input Image

Add PNG Image to Excel - Java POI


The different steps you have to follow to add PNG Image to Excel (XLSX) Workbook in Java POI, is provided below. (these steps were taken through in detail in our XLS Add Image tutorial  )

  • Create XLSX Workbook and Add a worksheet
  • Read input PNG / JPG Image into FileInputStream Object
  • Convert picture to be added into a byte array.
  • Add Picture to Workbook, Specify picture type as PNG and Get an Index.
  • Close the InputStream. We are ready to attach the image to workbook now.
  • Create Drawing in Worksheet of type XSSFDrawing, This will be the Container for the image.
  • Create XSSFClientAnchor Anchor point. - This will specify where exactly we have to position the Picture.
  • Invoke createPicture class, and specify the picture ID / Anchor details.
  • Write final XLSX file to OutputStream.
You can use the same steps for JPEG images also, just that you have to specify the image type properly so that POI can read this.

XLSX - Add Picture- POI Java Program Example


The complete Java program that adds a PNG / JPG Image into XLSX workbook is provided below:

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
public class AddImageExampleXLSX {  
        public static void main(String[] args) throws Exception{
                /* Create a Workbook and Worksheet */
                XSSFWorkbook my_workbook = new XSSFWorkbook();
                XSSFSheet my_sheet = my_workbook.createSheet("MyLogo");         
                /* Read input PNG / JPG Image into FileInputStream Object*/
                InputStream my_banner_image = new FileInputStream("input.png");
                /* Convert picture to be added into a byte array */
                byte[] bytes = IOUtils.toByteArray(my_banner_image);
                /* Add Picture to Workbook, Specify picture type as PNG and Get an Index */
                int my_picture_id = my_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
                /* Close the InputStream. We are ready to attach the image to workbook now */
                my_banner_image.close();                
                /* Create the drawing container */
                XSSFDrawing drawing = my_sheet.createDrawingPatriarch();
                /* Create an anchor point */
                XSSFClientAnchor my_anchor = new XSSFClientAnchor();
                /* Define top left corner, and we can resize picture suitable from there */
                my_anchor.setCol1(2);
                my_anchor.setRow1(1);           
                /* Invoke createPicture and pass the anchor point and ID */
                XSSFPicture  my_picture = drawing.createPicture(my_anchor, my_picture_id);
                /* Call resize method, which resizes the image */
                my_picture.resize();            
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\insert_png_xlsx_example.xlsx"));
                my_workbook.write(out);
                out.close();
        }
}

The output of this program is shown below. As you can see, the PNG image is added to the workbook successfully.

XLSX - Add PNG Image to Workbook - Java POI Example - Output
XLSX - Add PNG Image to Workbook - Java POI Example - Output
That completes the tutorial on adding images to XLSX workbooks in POI. We will see more interesting examples as we move on.

No comments:

Post a Comment