You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by MobileAutomator <Mo...@gmail.com> on 2012/07/30 08:07:00 UTC

Inserting Image To a existing workbook

Hi, 

I am trying to insert a image to a and existing work book. Below is the code
for that

	public void insertPicture(String file, String Image) throws Exception{
		
		FileInputStream myInput = new FileInputStream(file);
		POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
		
		HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
		 HSSFSheet mySheet;
		 
		 
		 mySheet = myWorkBook.getSheetAt(myWorkBook.getNumberOfSheets()-1);
		
		 int rownum = mySheet.getLastRowNum()+1; 
		
		 HSSFRow r1 = mySheet.createRow(rownum);
		 
		 System.out.println("Row Number  : " + rownum);
		 
		FileInputStream fis=new FileInputStream(Image);
		ByteArrayOutputStream img_bytes=new ByteArrayOutputStream();
		int b;
		while((b=fis.read())!=-1)
		img_bytes.write(b);
		fis.close();
		
		
		HSSFClientAnchor anchor = new
HSSFClientAnchor(0,0,0,0,(short)1,rownum,(short)2,++rownum);
	
		int
index=myWorkBook.addPicture(img_bytes.toByteArray(),HSSFWorkbook.PICTURE_TYPE_PNG);
		
		HSSFPatriarch patriarch=mySheet.createDrawingPatriarch();
		patriarch.createPicture(anchor,index);
		anchor.setAnchorType(2);
				
		 try{
             FileOutputStream out = new FileOutputStream(file);
             myWorkBook.write(out);
             out.close();
         }catch(Exception e){ 
        	 e.printStackTrace();
         } 
		
	}

and i get the below exception.

java.lang.IndexOutOfBoundsException: Index: 6, Size: 1
	at java.util.ArrayList.RangeCheck(ArrayList.java:547)
	at java.util.ArrayList.get(ArrayList.java:322)
	at
org.apache.poi.hssf.model.DrawingManager2.getDrawingGroup(DrawingManager2.java:125)
	at
org.apache.poi.hssf.model.DrawingManager2.allocateShapeId(DrawingManager2.java:71)
	at
org.apache.poi.hssf.record.EscherAggregate.convertPatriarch(EscherAggregate.java:936)
	at
org.apache.poi.hssf.record.EscherAggregate.convertUserModelToRecords(EscherAggregate.java:769)
	at
org.apache.poi.hssf.record.EscherAggregate.getRecordSize(EscherAggregate.java:507)
	at
org.apache.poi.hssf.model.InternalSheet.preSerialize(InternalSheet.java:1565)
	at
org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:1289)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1209)
	at com.excel.ReadWrite.ExcelWrite.insertPicture(ExcelWrite.java:590)
	at com.excel.ReadWrite.ExcelWrite.main(ExcelWrite.java:998)


I guess the index 6 is the number of sheets in the workbook as i have 6
sheets in my workbook.


I am using poi- 3.8



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Inserting-Image-To-a-existing-workbook-tp5710574.html
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


Re: Inserting Image To a existing workbook

Posted by Evgeniy Berlog <su...@gmail.com>.
Hi,

I successfully run your code on my computer using POI 3.8.
Can you please send me your source .xls file to which you add image?

Regards, Evgeniy



On Mon, Jul 30, 2012 at 9:07 AM, MobileAutomator
<Mo...@gmail.com>wrote:

> Hi,
>
> I am trying to insert a image to a and existing work book. Below is the
> code
> for that
>
>         public void insertPicture(String file, String Image) throws
> Exception{
>
>                 FileInputStream myInput = new FileInputStream(file);
>                 POIFSFileSystem myFileSystem = new
> POIFSFileSystem(myInput);
>
>                 HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
>                  HSSFSheet mySheet;
>
>
>                  mySheet =
> myWorkBook.getSheetAt(myWorkBook.getNumberOfSheets()-1);
>
>                  int rownum = mySheet.getLastRowNum()+1;
>
>                  HSSFRow r1 = mySheet.createRow(rownum);
>
>                  System.out.println("Row Number  : " + rownum);
>
>                 FileInputStream fis=new FileInputStream(Image);
>                 ByteArrayOutputStream img_bytes=new
> ByteArrayOutputStream();
>                 int b;
>                 while((b=fis.read())!=-1)
>                 img_bytes.write(b);
>                 fis.close();
>
>
>                 HSSFClientAnchor anchor = new
> HSSFClientAnchor(0,0,0,0,(short)1,rownum,(short)2,++rownum);
>
>                 int
>
> index=myWorkBook.addPicture(img_bytes.toByteArray(),HSSFWorkbook.PICTURE_TYPE_PNG);
>
>                 HSSFPatriarch patriarch=mySheet.createDrawingPatriarch();
>                 patriarch.createPicture(anchor,index);
>                 anchor.setAnchorType(2);
>
>                  try{
>              FileOutputStream out = new FileOutputStream(file);
>              myWorkBook.write(out);
>              out.close();
>          }catch(Exception e){
>                  e.printStackTrace();
>          }
>
>         }
>
> and i get the below exception.
>
> java.lang.IndexOutOfBoundsException: Index: 6, Size: 1
>         at java.util.ArrayList.RangeCheck(ArrayList.java:547)
>         at java.util.ArrayList.get(ArrayList.java:322)
>         at
>
> org.apache.poi.hssf.model.DrawingManager2.getDrawingGroup(DrawingManager2.java:125)
>         at
>
> org.apache.poi.hssf.model.DrawingManager2.allocateShapeId(DrawingManager2.java:71)
>         at
>
> org.apache.poi.hssf.record.EscherAggregate.convertPatriarch(EscherAggregate.java:936)
>         at
>
> org.apache.poi.hssf.record.EscherAggregate.convertUserModelToRecords(EscherAggregate.java:769)
>         at
>
> org.apache.poi.hssf.record.EscherAggregate.getRecordSize(EscherAggregate.java:507)
>         at
>
> org.apache.poi.hssf.model.InternalSheet.preSerialize(InternalSheet.java:1565)
>         at
> org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:1289)
>         at
> org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1209)
>         at
> com.excel.ReadWrite.ExcelWrite.insertPicture(ExcelWrite.java:590)
>         at com.excel.ReadWrite.ExcelWrite.main(ExcelWrite.java:998)
>
>
> I guess the index 6 is the number of sheets in the workbook as i have 6
> sheets in my workbook.
>
>
> I am using poi- 3.8
>
>
>
> --
> View this message in context:
> http://apache-poi.1045710.n5.nabble.com/Inserting-Image-To-a-existing-workbook-tp5710574.html
> 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
>
>