You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by "spencer.c" <sp...@gmail.com> on 2007/07/19 16:10:24 UTC

Adding Picture to Excel Sheet using POI not working.

Hello, I'm using POI 3.0.1 final, and trying to add a picture to an excel
sheet that I've got.  I've gone through the archives, and it really seems to
me that the following code ought to be working.  If someone could run an eye
over it, and tell me where I may be going astray, that would be great.  

The code runs, and throws no errors, seems to finish correctly.  But when I
open the sheet, there is no image there.  I added the bit in the middle to
make sure that the bytes for the image are in fact correct, and they are, I
am able to open the written image file without problem.  

    private void embedImage( HSSFWorkbook wb )
    {
        byte[] imgBytes = source.getImageBytes();

        try {
            File f = new File("C:\\output.jpg");
            f.createNewFile();

            FileOutputStream fos = new FileOutputStream(f);
            fos.write(imgBytes);
            fos.flush();
            fos.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }



        HSSFPatriarch patr = wb.getSheetAt(0).createDrawingPatriarch();
        HSSFClientAnchor a = new HSSFClientAnchor( 1,1,1,1, (short)0, 0,
(short)1, (short)1 );
        a.setAnchorType(2);

        int imgHandle = wb.addPicture(imgBytes,
HSSFWorkbook.PICTURE_TYPE_JPEG);
        
        HSSFSimpleShape shape = patr.createPicture(a, imgHandle);
        
        //shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_PICTURE);
    }

I've tried the code both with the last line active and commented out,
without visible effect.  Is there anything else I should need to do to add
the image?  Is there anything else I can check in the worksheet itself, or
in the debugger for additional information?  As I say, from all appearances
in the debugger, this works fine, it just doesn't show when I open the
spreadsheet.

Thanks for your assistance!

-- 
View this message in context: http://www.nabble.com/Adding-Picture-to-Excel-Sheet-using-POI-not-working.-tf4111349.html#a11690130
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org