Add Images in Worksheet

Aspose.Cells - Add Images in Worksheet

The Picture class is available to handle images in a worksheet.

Java

 // Get the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);

// Insert a string value to a cell
worksheet.getCells().get("C2").setValue("Image");

// Set the 4th row height
worksheet.getCells().setRowHeight(3, 150);

// Set the C column width
worksheet.getCells().setColumnWidth(2, 50);

// Add a picture to the C4 cell
int index = worksheet.getPictures().add(3, 2, 3, 2, dataDir + "aspose.jpg");

// Get the picture object
Picture pic = worksheet.getPictures().get(index);

// Set the placement type
pic.setPlacement(PlacementType.FREE_FLOATING);

Apache POI SS - HSSF XSSF - Add Images in Worksheet

The Picture class is available to handle images in a worksheet.

Java

 // Add picture data to this workbook.
InputStream is = new FileInputStream(dataDir + "aspose.jpg");
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
is.close();

CreationHelper helper = wb.getCreationHelper();

// Create sheet
Sheet sheet = wb.createSheet();

// Create the drawing patriarch. This is the top‑level container for all shapes.
Drawing drawing = sheet.createDrawingPatriarch();

// Add a picture shape
ClientAnchor anchor = helper.createClientAnchor();

// Set top‑left corner of the picture,
// subsequent call to 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();

Download Running Code

Download Sample Code