hssf poi
Have you never inserted an image in excel using POI if so
would you be so nice to share a code excerpt sample with me?
I haver acode snnipet and I simple can't get it to work
would you aid me improving it or tell me what I'm doing wrong?
THX
HSSFPatriarch patriarch=sheet.createDrawingPatriarch();
HSSFClientAnchor anchor;
anchor=new HSSFClientAnchor(0,0,0,255,(short)0,5,(short)0,8);
anchor.setAnchorType(2);
patriarch.createPicture(anchor,loadPicture("/usr/image/img.png",workBook);
Is the path to the image correct?
It's really sad but the POI documentation and quick guide contains loads of errors. The method you are describing patriarch.createPicture()
is not fully implemented although it is mentioned in the quick guide that it is fully functional.
There is a way around that though for inserting images into POI:
load your image into a FileInputStream and use the method
HSSFWorkbook.addPicture(FileInputStream, type)// type is a constant. Look at POI javadocs for the type of image constant you need there
The above will return an image index of type int.
then do:
HSSFClientAnchor anchor = new HSSFClientAnchor( 0, 0, 0, 0, col, row, ++col, ++row );
anchor.setAnchorType( 2 );
patriarch.createPicture( anchor, imgIndex );
//Image index above is the index number that you have from the previous method.
This worked for me, and I hope it works for you
Hiya:
thanks for the reply Gorteo:
finally I went through expecting it all came up I've gone further the error
with the hint you gave me, however the image won't show up I've made certain and the path is right what else could be?
this is my code snippet:
String path="C:\\WebSphere\\AppServer\\installedApps\\AnalisisPagos.ear\\AnalisisPagosWeb.war\\imagenes\\logo1.png";
FileInputStream fimage=null;
ByteArrayOutputStream bos=null;
try{
fimage=new FileInputStream(path);
bos = new ByteArrayOutputStream( );
int c;
while ( (c = fimage.read()) != -1)
bos.write( c );
}catch(IOException e){
e.printStackTrace();
System.out.println(e);
}
int imgindex=workBook.addPicture(bos.toByteArray(),HSSFWorkbook.PICTURE_TYPE_PNG);
HSSFPatriarch patriarch=sheet.createDrawingPatriarch();
HSSFClientAnchor anchor;
anchor=new HSSFClientAnchor(0,0,0,255,(short)0,5,(short)0,8);
anchor.setAnchorType(2);
patriarch.createPicture(anchor, imgindex);
Did you try passing dierctly the FileInputStream instead of a ByteArray()?You shouldn't need to have a ByteOutputStream. The fileInputStream is sufficient. Try passing a FileInputStream. Also are you getting an image index int back?
Hi Gorteo still trying my very best with no success .
interesting inquiry the one you post:
***Did you try passing dierctly the FileInputStream instead of a ByteArray()?
HOW CAN I ..?
if the function overload does not accept an FileInputStream as argument
int imgindex=workBook.addPicture(bos.toByteArray(),HSSFWorkbook.PICTURE_TYPE_PNG);
It does need a toByteArray
ok man try the following:
File pic = new File( currentPath );
long length = pic.length( );
picData = new byte[ ( int ) length ];
FileInputStream picIn = new FileInputStream( pic );
picIn.read( picData );
int index=workbook.addPicture( picData, type );
The rest of the code (calling the Anchor and patriacrh) leave them as you have them.
Make sure you are getting an index number back.
I hope this helps. Keep me posted with results.
Good luck
Sorry man I forgot the byte array declaration:
byte[] picData = null;
File pic = new File( currentPath );
long length = pic.length( );
picData = new byte[ ( int ) length ];
FileInputStream picIn = new FileInputStream( pic );
picIn.read( picData );
int index=workbook.addPicture( picData, type );
That should do the trick
ok that works terrific.Thank yuo So Much
Happy I could help. Beware that there is a trick in that image insertation method though.... It inserts the image but it also resize the image to fit in the cell (as you might have already discovered).
So if your cell is quite big expect a quite big image there which will cover all the cell.
There is another method I wrote which you can play with floating images but then you have to start calculating cell pixels which can get really messy.
Cheers
Hi,I saw the thread regarding adding an image to Excel using POI.But I am not able to find the API in the latest release.Could you please let me know where can I find the related release.Thanks.
I am trying to use the code snipt you and Georg. posted but getting an error message as under.
** HSSFWorkbook.PICTURE_TYPE_PNG cannot be resolved
can you please tell me why is this? and guide me whether I am using the right version of the API? or some other mistake?
Can you please provide me the code sample and the version of API you are using on this email ID: suddin@telespectrum.com
Thanks
Sabah u din Irfan
> can you please tell me why is this? and guide me> whether I am using the right version of the API? or> some other mistake?You are probably using an older version of POI. Are you sure you downloaded the latest version?
you need to download the dev-version!the release-version does not contain the api mentioned above.
how may i do this with URL?
is there any problem with me code?
URL url = new URL(path);
InputStream is = url.openConnection().getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream( );
int r;
byte[] buffer = new byte[8000];
while ((r = is.read(buffer)) >= 0) {
if (r == 0) continue;
bos.write(buffer, 0, r);
}
img_index = wb.addPicture( bos.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG);