Jump to content

How can I embed multiple images in Excel using POI?

peeps's Photo
Posted Jul 15 2010 03:45 AM
4825 Views

Hi,

I am able to embed an image to a sheet. But, when I try to embed multiple images to the same sheet giving different anchors, I notice my excel generated with the second image loaded, first image is not displayed. Below is the code I've used to insert images to the sheet. Please guide me if there is anything wrong in the code.

public static void addChart(String[] chartFiles, HSSFWorkbook wb, HSSFSheet sheet,int row){
//add chart to this workbook.

int col=0;
try {
for (int i = 0; i < chartFiles.length -1; i++) {
InputStream is = new FileInputStream(chartFiles[0]);
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
is.close();

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

//add a picture shape
HSSFClientAnchor anchor = new HSSFClientAnchor();
//set top-left corner of the picture,
//subsequent call of Picture#resize() will operate relative to it
anchor.setCol1((short)col);
anchor.setCol2((short)3);
anchor.setRow1(row);

HSSFPicture pict = drawing.createPicture(anchor, pictureIdx);

//auto-size picture relative to its top-left corner
pict.resize();
row+=25;
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}

Thanks,
pp

Tags:
0 Subscribe


4 Replies

0
  alagu k's Photo
Posted Nov 08 2010 03:08 AM

hi peeps,

Am also facing the same problem which u ve faced.
Only the second image is displayed. First image is not displayed.
do you have any solution now?
its very much required for me to continue with my project.

please reply as soon as possible.

Thanks in advance :rolleyes:
Alagu
0
  Sireesha Ambati's Photo
Posted Oct 27 2011 11:10 AM

Hi Peeps,
I am also facing the same issue. Did you get any solution for this problem?Please let me know if you know any.

Thanks in Advance
Sireesha
0
  lepolochon's Photo
Posted Dec 02 2011 08:44 AM

Hi everyboby,

I am facing exactly the same issue: only the last picture is displayed. The previous pictures that are also in the excel sheet are lost.
If you have solved the issue, please tell me !!


Thanks,
Paul
0
  Khode Prasad's Photo
Posted Feb 08 2012 02:46 AM

its simple

do not create a new instance of HSSFPatriarch when embedding second image, i.e.

do not call HSSFPatriarch drawing = sheet.createDrawingPatriarch(); method, just use the existing HSSFPatriarch instance for the other images.
Khode Prasad