Hi All,

 

I've been trying to add multiple images on one excel sheet for a few days
now and I'm running out of options.

 

Could anyone please take a look at the code below and tell me why only 1
picture gets added to the sheet? The picture added is the last one that
needs to be added, the rest is just gone (or was never added in the first
place).

 

This is my code:

 

      /**

       * Adds an image to the given cell.

       *

       * @param sheet The sheet to add the image to.

       * @param wb The workbook to add the image to.

       * @param row The row index (0-based)

       * @param columnIndex The column index (0-based)

       * @param bis The [EMAIL PROTECTED] ByteArrayInputStream} of the image to 
add

       */

      private void addThumbnail(HSSFSheet sheet, HSSFWorkbook wb, HSSFRow
row, int columnIndex, ByteArrayInputStream bis) {

            HSSFPatriarch apatriarch = sheet.createDrawingPatriarch();

            HSSFClientAnchor a = new HSSFClientAnchor(0, 0, 1023, 255,

                        (short) columnIndex, row.getRowNum(), (short)
columnIndex, row

                                   .getRowNum());

 

            HSSFPicture picture = apatriarch.createPicture(a,
loadPicture(bis, wb));

            Dimension d = picture.getImageDimension();

            row.setHeightInPoints((short) d.getHeight());

            picture.resize();

 

            float anchorHeight = a.getAnchorHeightInPoints(sheet);

            row.setHeightInPoints(anchorHeight);

            picture.resize();

      }

 

      /**

       * Loads the picture in the workbook and returns its index.

       *

       * @param bis The ByteArrayInputStream of the image.

       * @param wb The workbook to add the picture to.

       * @return The index of the image.

       * @throws IOException When the

       */

      private int loadPicture(ByteArrayInputStream bis, HSSFWorkbook wb)  {

            int pictureIndex;

            ByteArrayOutputStream bos = null;

            try {

                  bos = new ByteArrayOutputStream();

                  int c;

                  while ((c = bis.read()) != -1)

                        bos.write(c);

                  pictureIndex = wb.addPicture(bos.toByteArray(),

                             HSSFWorkbook.PICTURE_TYPE_JPEG);

            } finally {

                  if (bis != null)

                        try {

                             bis.close();

                        }

                        catch (IOException ioe) {

                             LOGGER.warn("Couldn't close byte input stream "
+

                                         "while adding picture to
workbook.");

                        }

                  if (bos != null)

                        try {

                             bos.close();

                        }

                        catch (IOException ioe) {

                             LOGGER.warn("Couldn't close byte output stream
" +

                                         "while adding picture to
workbook.");

                        }

            }

            return pictureIndex;

      }

 

Any help is greatly appreciated!

 

Thanks in advance,

Kees.

Reply via email to