Garudadwajan Garudadwajan - 1 year ago 101
Java Question

how to add images in HSSFCell in apache POI?

How to add Image in different different HSSFCell object in poi ?

I have written some code which is adding image but problem is, the cell were I added last image, That cell only showing image other than that no other cells are showing images ...

appreciate your help ...

My Code is


HSSFCell cell = getHSSFCell(sheet, rowNo, cellNo);

cell.setCellValue(new HSSFRichTextString(rs.getString("TEST_STEP_DETAILS")) );

String annotate = rs.getString("ANNOTATE");

if(annotate != null){
int index = getPicIndex(wb);
HSSFPatriarch patriarch=sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(400,10,655,200,(short)cellNo,(rowNo+1),(short)cellNo,(rowNo+1));
patriarch.createPicture(anchor, index);

getPicIndex METHOD :-

public static int getPicIndex(HSSFWorkbook wb){
int index = -1;
try {
byte[] picData = null;
File pic = new File( "C:\\pdf\\logo.jpg" );
long length = pic.length( );
picData = new byte[ ( int ) length ];
FileInputStream picIn = new FileInputStream( pic ); picData );
index = wb.addPicture( picData, HSSFWorkbook.PICTURE_TYPE_JPEG );
} catch (IOException e) {
} catch (Exception e) {
return index;

Answer Source

i hope you found the solution yourself. if not:
the problem is that you create for every image a new partiarch. HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
you should create only one patriarch instance and use its createPicture method for all images.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download