You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Felipe Issa <fe...@simova.com.br> on 2012/01/20 13:10:46 UTC

Can't insert picture on a XSSFWorkbook

Hi. I'm using apache poi 3.8 beta 5 (20111217) and i can't get it to 
insert a picture on a XSSFWorkbook. It works just fine when i try to 
insert it on a HSSFWorkbook, but when i change the type of the file it 
doesn't show anything on the sheet.  Does anyone knows how to fix this? 
Is it a bug?

Here is the code i'm using:

public void insertPicture() throws Exception{
         /*
          * 0 = HSSFWorkbook
          * anything else = XSSFWorkbook
          */
         int type = 1;

         Workbook wb = null;

         if(type == 0){
             wb = new HSSFWorkbook();
         }else{
             wb = new XSSFWorkbook();
         }
         CreationHelper createHelper = wb.getCreationHelper();

         StringBuffer sb = new StringBuffer();
         sb.append("C:\\temp\\");
         File f = new File(sb.toString());
         if (!f.isDirectory()) {
             f.mkdirs();
         }

         String dataHoje = new 
SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Util.getCurrentDate());
         sb.append("file_");
         sb.append(dataHoje);
         if(type == 0){
             sb.append(".xls");
         }else{
             sb.append(".xlsx");
         }


         File arquivo = new File(sb.toString());
         if (!arquivo.exists()) {
             arquivo.createNewFile();
         }

         FileOutputStream outputStream;
         Sheet sheet = null;

         try {
             outputStream =  new FileOutputStream(arquivo);
             sheet = wb.createSheet();
             Drawing patriarch= sheet.createDrawingPatriarch();
             ClientAnchor anchor = createHelper.createClientAnchor();
             anchor.setCol1(4);
             anchor.setCol2(4);
             anchor.setRow1(0);
             anchor.setRow2(0);
             anchor.setDx1(400);
             anchor.setDx2(655);
             anchor.setDy1(10);
             anchor.setDy2(200);

             anchor.setAnchorType(1);
             File pic = new File("C:\\Temp\\img_ok.png");
             long length = pic.length(  );
             byte[]picData = new byte[ ( int ) length ];
             FileInputStream picIn = new FileInputStream( pic );
             picIn.read(picData);
             int index = wb.addPicture(picData, wb.PICTURE_TYPE_PNG);
             patriarch.createPicture(anchor,index);


             wb.write(outputStream);
             outputStream.close();
         } catch (Exception e) {
             throw e;
         }
     }

Thanks in advice!

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


Re: Can't insert picture on a XSSFWorkbook

Posted by Felipe Issa <fe...@simova.com.br>.
too many lists. Sry about that
Em 20/01/2012 10:13, Christian Grobmeier escreveu:
> wrong list, use user@poi.apache.org instead
>
> Cheers
> Christian
>

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


Re: Can't insert picture on a XSSFWorkbook

Posted by Christian Grobmeier <gr...@gmail.com>.
wrong list, use user@poi.apache.org instead

Cheers
Christian

On Fri, Jan 20, 2012 at 1:10 PM, Felipe Issa <fe...@simova.com.br> wrote:
> Hi. I'm using apache poi 3.8 beta 5 (20111217) and i can't get it to insert
> a picture on a XSSFWorkbook. It works just fine when i try to insert it on a
> HSSFWorkbook, but when i change the type of the file it doesn't show
> anything on the sheet.  Does anyone knows how to fix this? Is it a bug?
>
> Here is the code i'm using:
>
> public void insertPicture() throws Exception{
>        /*
>         * 0 = HSSFWorkbook
>         * anything else = XSSFWorkbook
>         */
>        int type = 1;
>
>        Workbook wb = null;
>
>        if(type == 0){
>            wb = new HSSFWorkbook();
>        }else{
>            wb = new XSSFWorkbook();
>        }
>        CreationHelper createHelper = wb.getCreationHelper();
>
>        StringBuffer sb = new StringBuffer();
>        sb.append("C:\\temp\\");
>        File f = new File(sb.toString());
>        if (!f.isDirectory()) {
>            f.mkdirs();
>        }
>
>        String dataHoje = new
> SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Util.getCurrentDate());
>        sb.append("file_");
>        sb.append(dataHoje);
>        if(type == 0){
>            sb.append(".xls");
>        }else{
>            sb.append(".xlsx");
>        }
>
>
>        File arquivo = new File(sb.toString());
>        if (!arquivo.exists()) {
>            arquivo.createNewFile();
>        }
>
>        FileOutputStream outputStream;
>        Sheet sheet = null;
>
>        try {
>            outputStream =  new FileOutputStream(arquivo);
>            sheet = wb.createSheet();
>            Drawing patriarch= sheet.createDrawingPatriarch();
>            ClientAnchor anchor = createHelper.createClientAnchor();
>            anchor.setCol1(4);
>            anchor.setCol2(4);
>            anchor.setRow1(0);
>            anchor.setRow2(0);
>            anchor.setDx1(400);
>            anchor.setDx2(655);
>            anchor.setDy1(10);
>            anchor.setDy2(200);
>
>            anchor.setAnchorType(1);
>            File pic = new File("C:\\Temp\\img_ok.png");
>            long length = pic.length(  );
>            byte[]picData = new byte[ ( int ) length ];
>            FileInputStream picIn = new FileInputStream( pic );
>            picIn.read(picData);
>            int index = wb.addPicture(picData, wb.PICTURE_TYPE_PNG);
>            patriarch.createPicture(anchor,index);
>
>
>            wb.write(outputStream);
>            outputStream.close();
>        } catch (Exception e) {
>            throw e;
>        }
>    }
>
> Thanks in advice!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>



-- 
http://www.grobmeier.de
https://www.timeandbill.de

Re: Can't insert picture on a XSSFWorkbook

Posted by Felipe Issa <fe...@simova.com.br>.
Just tried it, had the same result.

Em 23/01/2012 12:16, Helfi escreveu:
> Have you tried the patriach.createAnchor-Method?
>
> I got something in my mind that there was a problem with the helper-anchor,
> but not for sure.
>
> --
> View this message in context: http://apache-poi.1045710.n5.nabble.com/Can-t-insert-picture-on-a-XSSFWorkbook-tp5160255p5166171.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
>
>


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


Re: Can't insert picture on a XSSFWorkbook

Posted by Helfi <st...@t-online.de>.
Have you tried the patriach.createAnchor-Method?

I got something in my mind that there was a problem with the helper-anchor,
but not for sure.

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Can-t-insert-picture-on-a-XSSFWorkbook-tp5160255p5166171.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: Can't insert picture on a XSSFWorkbook

Posted by Felipe Issa <fe...@simova.com.br>.
Bug created.

https://issues.apache.org/bugzilla/show_bug.cgi?id=52504

Thanks!

Em 23/01/2012 11:24, Yegor Kozlov escreveu:
> It looks like a bug in XSSF. Can you create a Bugzilla ticket and post
> a sample code that shows the trouble ?
>
> Yegor
>
> On Mon, Jan 23, 2012 at 2:37 PM, Felipe Issa<fe...@simova.com.br>  wrote:
>> I have tried that, but when I call resize() it resets the dx1 and dy1 to 0,
>> and that is not what I want! I need the picture to be in the middle of the
>> Cell.
>>
>> Felipe
>> Em 20/01/2012 11:26, Yegor Kozlov escreveu:
>>
>>> You need to call Picture.resize() in the end:
>>>
>>> Picture pict = drawing.createPicture(anchor, pictureIdx)
>>> pict.resize();
>>>
>>> Yegor
>>>
>>> On Fri, Jan 20, 2012 at 4:35 PM, Felipe Issa<fe...@simova.com.br>
>>>   wrote:
>>>> Hi. I'm using apache poi 3.8 beta 5 (20111217) and i can't get it to
>>>> insert
>>>> a picture on a XSSFWorkbook.
>>>> It works just fine when i try to insert it on a HSSFWorkbook, but when i
>>>> change the type of the file it doesn't show anything on the sheet.
>>>> Does anyone knows how to fix this? Is it a bug?
>>>>
>>>> Here is the code i'm using:
>>>>
>>>> public void insertPicture() throws Exception{
>>>>         /*
>>>>          * 0 = HSSFWorkbook
>>>>          * anything else = XSSFWorkbook
>>>>          */
>>>>         int type = 1;
>>>>
>>>>         Workbook wb = null;
>>>>
>>>>         if(type == 0){
>>>>             wb = new HSSFWorkbook();
>>>>         }else{
>>>>             wb = new XSSFWorkbook();
>>>>         }
>>>>         CreationHelper createHelper = wb.getCreationHelper();
>>>>
>>>>         StringBuffer sb = new StringBuffer();
>>>>         sb.append("C:\\temp\\");
>>>>         File f = new File(sb.toString());
>>>>         if (!f.isDirectory()) {
>>>>             f.mkdirs();
>>>>         }
>>>>
>>>>         String dataHoje = new
>>>> SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Util.getCurrentDate());
>>>>         sb.append("file_");
>>>>         sb.append(dataHoje);
>>>>         if(type == 0){
>>>>             sb.append(".xls");
>>>>         }else{
>>>>             sb.append(".xlsx");
>>>>         }
>>>>
>>>>
>>>>         File arquivo = new File(sb.toString());
>>>>         if (!arquivo.exists()) {
>>>>             arquivo.createNewFile();
>>>>         }
>>>>
>>>>         FileOutputStream outputStream;
>>>>         Sheet sheet = null;
>>>>
>>>>         try {
>>>>             outputStream =  new FileOutputStream(arquivo);
>>>>             sheet = wb.createSheet();
>>>>             Drawing patriarch= sheet.createDrawingPatriarch();
>>>>             ClientAnchor anchor = createHelper.createClientAnchor();
>>>>             anchor.setCol1(4);
>>>>             anchor.setCol2(4);
>>>>             anchor.setRow1(0);
>>>>             anchor.setRow2(0);
>>>>             anchor.setDx1(400);
>>>>             anchor.setDx2(655);
>>>>             anchor.setDy1(10);
>>>>             anchor.setDy2(200);
>>>>
>>>>             anchor.setAnchorType(1);
>>>>             File pic = new File("C:\\Temp\\img_ok.png");
>>>>             long length = pic.length(  );
>>>>             byte[]picData = new byte[ ( int ) length ];
>>>>             FileInputStream picIn = new FileInputStream( pic );
>>>>             picIn.read(picData);
>>>>             int index = wb.addPicture(picData, wb.PICTURE_TYPE_PNG);
>>>>             patriarch.createPicture(anchor,index);
>>>>
>>>>
>>>>             wb.write(outputStream);
>>>>             outputStream.close();
>>>>         } catch (Exception e) {
>>>>             throw e;
>>>>         }
>>>>     }
>>>>
>>>> Thanks in advice!
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>>>> For additional commands, e-mail: user-help@poi.apache.org
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>>> For additional commands, e-mail: user-help@poi.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>> For additional commands, e-mail: user-help@poi.apache.org
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>


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


Re: Can't insert picture on a XSSFWorkbook

Posted by Yegor Kozlov <ye...@dinom.ru>.
It looks like a bug in XSSF. Can you create a Bugzilla ticket and post
a sample code that shows the trouble ?

Yegor

On Mon, Jan 23, 2012 at 2:37 PM, Felipe Issa <fe...@simova.com.br> wrote:
> I have tried that, but when I call resize() it resets the dx1 and dy1 to 0,
> and that is not what I want! I need the picture to be in the middle of the
> Cell.
>
> Felipe
> Em 20/01/2012 11:26, Yegor Kozlov escreveu:
>
>> You need to call Picture.resize() in the end:
>>
>> Picture pict = drawing.createPicture(anchor, pictureIdx)
>> pict.resize();
>>
>> Yegor
>>
>> On Fri, Jan 20, 2012 at 4:35 PM, Felipe Issa<fe...@simova.com.br>
>>  wrote:
>>>
>>> Hi. I'm using apache poi 3.8 beta 5 (20111217) and i can't get it to
>>> insert
>>> a picture on a XSSFWorkbook.
>>> It works just fine when i try to insert it on a HSSFWorkbook, but when i
>>> change the type of the file it doesn't show anything on the sheet.
>>> Does anyone knows how to fix this? Is it a bug?
>>>
>>> Here is the code i'm using:
>>>
>>> public void insertPicture() throws Exception{
>>>        /*
>>>         * 0 = HSSFWorkbook
>>>         * anything else = XSSFWorkbook
>>>         */
>>>        int type = 1;
>>>
>>>        Workbook wb = null;
>>>
>>>        if(type == 0){
>>>            wb = new HSSFWorkbook();
>>>        }else{
>>>            wb = new XSSFWorkbook();
>>>        }
>>>        CreationHelper createHelper = wb.getCreationHelper();
>>>
>>>        StringBuffer sb = new StringBuffer();
>>>        sb.append("C:\\temp\\");
>>>        File f = new File(sb.toString());
>>>        if (!f.isDirectory()) {
>>>            f.mkdirs();
>>>        }
>>>
>>>        String dataHoje = new
>>> SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Util.getCurrentDate());
>>>        sb.append("file_");
>>>        sb.append(dataHoje);
>>>        if(type == 0){
>>>            sb.append(".xls");
>>>        }else{
>>>            sb.append(".xlsx");
>>>        }
>>>
>>>
>>>        File arquivo = new File(sb.toString());
>>>        if (!arquivo.exists()) {
>>>            arquivo.createNewFile();
>>>        }
>>>
>>>        FileOutputStream outputStream;
>>>        Sheet sheet = null;
>>>
>>>        try {
>>>            outputStream =  new FileOutputStream(arquivo);
>>>            sheet = wb.createSheet();
>>>            Drawing patriarch= sheet.createDrawingPatriarch();
>>>            ClientAnchor anchor = createHelper.createClientAnchor();
>>>            anchor.setCol1(4);
>>>            anchor.setCol2(4);
>>>            anchor.setRow1(0);
>>>            anchor.setRow2(0);
>>>            anchor.setDx1(400);
>>>            anchor.setDx2(655);
>>>            anchor.setDy1(10);
>>>            anchor.setDy2(200);
>>>
>>>            anchor.setAnchorType(1);
>>>            File pic = new File("C:\\Temp\\img_ok.png");
>>>            long length = pic.length(  );
>>>            byte[]picData = new byte[ ( int ) length ];
>>>            FileInputStream picIn = new FileInputStream( pic );
>>>            picIn.read(picData);
>>>            int index = wb.addPicture(picData, wb.PICTURE_TYPE_PNG);
>>>            patriarch.createPicture(anchor,index);
>>>
>>>
>>>            wb.write(outputStream);
>>>            outputStream.close();
>>>        } catch (Exception e) {
>>>            throw e;
>>>        }
>>>    }
>>>
>>> Thanks in advice!
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>>> For additional commands, e-mail: user-help@poi.apache.org
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>> For additional commands, e-mail: user-help@poi.apache.org
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>

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


Re: Can't insert picture on a XSSFWorkbook

Posted by Felipe Issa <fe...@simova.com.br>.
I have tried that, but when I call resize() it resets the dx1 and dy1 to 
0, and that is not what I want! I need the picture to be in the middle 
of the Cell.

Felipe
Em 20/01/2012 11:26, Yegor Kozlov escreveu:
> You need to call Picture.resize() in the end:
>
> Picture pict = drawing.createPicture(anchor, pictureIdx)
> pict.resize();
>
> Yegor
>
> On Fri, Jan 20, 2012 at 4:35 PM, Felipe Issa<fe...@simova.com.br>  wrote:
>> Hi. I'm using apache poi 3.8 beta 5 (20111217) and i can't get it to insert
>> a picture on a XSSFWorkbook.
>> It works just fine when i try to insert it on a HSSFWorkbook, but when i
>> change the type of the file it doesn't show anything on the sheet.
>> Does anyone knows how to fix this? Is it a bug?
>>
>> Here is the code i'm using:
>>
>> public void insertPicture() throws Exception{
>>         /*
>>          * 0 = HSSFWorkbook
>>          * anything else = XSSFWorkbook
>>          */
>>         int type = 1;
>>
>>         Workbook wb = null;
>>
>>         if(type == 0){
>>             wb = new HSSFWorkbook();
>>         }else{
>>             wb = new XSSFWorkbook();
>>         }
>>         CreationHelper createHelper = wb.getCreationHelper();
>>
>>         StringBuffer sb = new StringBuffer();
>>         sb.append("C:\\temp\\");
>>         File f = new File(sb.toString());
>>         if (!f.isDirectory()) {
>>             f.mkdirs();
>>         }
>>
>>         String dataHoje = new
>> SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Util.getCurrentDate());
>>         sb.append("file_");
>>         sb.append(dataHoje);
>>         if(type == 0){
>>             sb.append(".xls");
>>         }else{
>>             sb.append(".xlsx");
>>         }
>>
>>
>>         File arquivo = new File(sb.toString());
>>         if (!arquivo.exists()) {
>>             arquivo.createNewFile();
>>         }
>>
>>         FileOutputStream outputStream;
>>         Sheet sheet = null;
>>
>>         try {
>>             outputStream =  new FileOutputStream(arquivo);
>>             sheet = wb.createSheet();
>>             Drawing patriarch= sheet.createDrawingPatriarch();
>>             ClientAnchor anchor = createHelper.createClientAnchor();
>>             anchor.setCol1(4);
>>             anchor.setCol2(4);
>>             anchor.setRow1(0);
>>             anchor.setRow2(0);
>>             anchor.setDx1(400);
>>             anchor.setDx2(655);
>>             anchor.setDy1(10);
>>             anchor.setDy2(200);
>>
>>             anchor.setAnchorType(1);
>>             File pic = new File("C:\\Temp\\img_ok.png");
>>             long length = pic.length(  );
>>             byte[]picData = new byte[ ( int ) length ];
>>             FileInputStream picIn = new FileInputStream( pic );
>>             picIn.read(picData);
>>             int index = wb.addPicture(picData, wb.PICTURE_TYPE_PNG);
>>             patriarch.createPicture(anchor,index);
>>
>>
>>             wb.write(outputStream);
>>             outputStream.close();
>>         } catch (Exception e) {
>>             throw e;
>>         }
>>     }
>>
>> Thanks in advice!
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>> For additional commands, e-mail: user-help@poi.apache.org
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>


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


Re: Can't insert picture on a XSSFWorkbook

Posted by Yegor Kozlov <ye...@dinom.ru>.
You need to call Picture.resize() in the end:

Picture pict = drawing.createPicture(anchor, pictureIdx)
pict.resize();

Yegor

On Fri, Jan 20, 2012 at 4:35 PM, Felipe Issa <fe...@simova.com.br> wrote:
> Hi. I'm using apache poi 3.8 beta 5 (20111217) and i can't get it to insert
> a picture on a XSSFWorkbook.
> It works just fine when i try to insert it on a HSSFWorkbook, but when i
> change the type of the file it doesn't show anything on the sheet.
> Does anyone knows how to fix this? Is it a bug?
>
> Here is the code i'm using:
>
> public void insertPicture() throws Exception{
>        /*
>         * 0 = HSSFWorkbook
>         * anything else = XSSFWorkbook
>         */
>        int type = 1;
>
>        Workbook wb = null;
>
>        if(type == 0){
>            wb = new HSSFWorkbook();
>        }else{
>            wb = new XSSFWorkbook();
>        }
>        CreationHelper createHelper = wb.getCreationHelper();
>
>        StringBuffer sb = new StringBuffer();
>        sb.append("C:\\temp\\");
>        File f = new File(sb.toString());
>        if (!f.isDirectory()) {
>            f.mkdirs();
>        }
>
>        String dataHoje = new
> SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Util.getCurrentDate());
>        sb.append("file_");
>        sb.append(dataHoje);
>        if(type == 0){
>            sb.append(".xls");
>        }else{
>            sb.append(".xlsx");
>        }
>
>
>        File arquivo = new File(sb.toString());
>        if (!arquivo.exists()) {
>            arquivo.createNewFile();
>        }
>
>        FileOutputStream outputStream;
>        Sheet sheet = null;
>
>        try {
>            outputStream =  new FileOutputStream(arquivo);
>            sheet = wb.createSheet();
>            Drawing patriarch= sheet.createDrawingPatriarch();
>            ClientAnchor anchor = createHelper.createClientAnchor();
>            anchor.setCol1(4);
>            anchor.setCol2(4);
>            anchor.setRow1(0);
>            anchor.setRow2(0);
>            anchor.setDx1(400);
>            anchor.setDx2(655);
>            anchor.setDy1(10);
>            anchor.setDy2(200);
>
>            anchor.setAnchorType(1);
>            File pic = new File("C:\\Temp\\img_ok.png");
>            long length = pic.length(  );
>            byte[]picData = new byte[ ( int ) length ];
>            FileInputStream picIn = new FileInputStream( pic );
>            picIn.read(picData);
>            int index = wb.addPicture(picData, wb.PICTURE_TYPE_PNG);
>            patriarch.createPicture(anchor,index);
>
>
>            wb.write(outputStream);
>            outputStream.close();
>        } catch (Exception e) {
>            throw e;
>        }
>    }
>
> Thanks in advice!
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>

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


Can't insert picture on a XSSFWorkbook

Posted by Felipe Issa <fe...@simova.com.br>.
Hi. I'm using apache poi 3.8 beta 5 (20111217) and i can't get it to insert a picture on a XSSFWorkbook.
It works just fine when i try to insert it on a HSSFWorkbook, but when i change the type of the file it doesn't show anything on the sheet.
Does anyone knows how to fix this? Is it a bug?

Here is the code i'm using:

public void insertPicture() throws Exception{
         /*
          * 0 = HSSFWorkbook
          * anything else = XSSFWorkbook
          */
         int type = 1;

         Workbook wb = null;

         if(type == 0){
             wb = new HSSFWorkbook();
         }else{
             wb = new XSSFWorkbook();
         }
         CreationHelper createHelper = wb.getCreationHelper();

         StringBuffer sb = new StringBuffer();
         sb.append("C:\\temp\\");
         File f = new File(sb.toString());
         if (!f.isDirectory()) {
             f.mkdirs();
         }

         String dataHoje = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Util.getCurrentDate());
         sb.append("file_");
         sb.append(dataHoje);
         if(type == 0){
             sb.append(".xls");
         }else{
             sb.append(".xlsx");
         }


         File arquivo = new File(sb.toString());
         if (!arquivo.exists()) {
             arquivo.createNewFile();
         }

         FileOutputStream outputStream;
         Sheet sheet = null;

         try {
             outputStream =  new FileOutputStream(arquivo);
             sheet = wb.createSheet();
             Drawing patriarch= sheet.createDrawingPatriarch();
             ClientAnchor anchor = createHelper.createClientAnchor();
             anchor.setCol1(4);
             anchor.setCol2(4);
             anchor.setRow1(0);
             anchor.setRow2(0);
             anchor.setDx1(400);
             anchor.setDx2(655);
             anchor.setDy1(10);
             anchor.setDy2(200);

             anchor.setAnchorType(1);
             File pic = new File("C:\\Temp\\img_ok.png");
             long length = pic.length(  );
             byte[]picData = new byte[ ( int ) length ];
             FileInputStream picIn = new FileInputStream( pic );
             picIn.read(picData);
             int index = wb.addPicture(picData, wb.PICTURE_TYPE_PNG);
             patriarch.createPicture(anchor,index);


             wb.write(outputStream);
             outputStream.close();
         } catch (Exception e) {
             throw e;
         }
     }

Thanks in advice!


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