Wiki

Clone wiki

Aspose for Apache POI / Add-Images-in-Worksheet

Apache POI SS

//add picture data to this workbook.
InputStream is = new FileInputStream(dataPath + "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 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();

Aspose.Cells

//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, dataPath + "aspose.jpg");

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

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

Download Source Code

Updated