0

To insert image to excel using POI:XSSF I am using maven poi dependency:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.8</version>
</dependency>

AND code as :

 InputStream my_banner_image = new FileInputStream("input.png");
 byte[] bytes = IOUtils.toByteArray(my_banner_image);
 int my_picture_id = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);

I am getting these errors:

1) The method toByteArray(InputStream) is undefined for the type IOUtils 2) PICTURE_TYPE_PNG cannot be resolved or is not a field

Any help would be appriciated. Thanks.

Nomad
  • 751
  • 4
  • 13
  • 34
  • Can you please share the complete stack trace. – Ankur Piyush Feb 10 '15 at 06:34
  • Why are you depending on such an old version of Apache POI? What happens when you specify `3.11` instead? (The latest stable version) – Gagravarr Feb 10 '15 at 11:23
  • @Gagravarr: did you try 3.11 ? and have you used XSSF with that? – Nomad Feb 10 '15 at 14:28
  • @AnkurPiyush: It is a compile error. The method toByteArray(InputStream) is undefined for the type IOUtils – Nomad Feb 10 '15 at 14:29
  • @guest I, along with huge numbers of others, use POI 3.11 with XSSF without issue – Gagravarr Feb 10 '15 at 14:51
  • @Gagravarr: Even after using POI 3.11 also facing same issue as: The method toByteArray(InputStream) is undefined for the type IOUtils – Nomad Feb 10 '15 at 17:10
  • It [really is there, see the javadocs](https://poi.apache.org/apidocs/org/apache/poi/util/IOUtils.html#toByteArray%28java.io.InputStream%29). Make sure you've imported the correct class - `org.apache.poi.util.IOUtils`, and don't have any older poi jars on your classpath – Gagravarr Feb 10 '15 at 17:34
  • @Gagravarr: thanks. I had some older poi versions, I removed those and worked fine. One quick question:How can i add multiple fonts to a single cell text. like as the text to fit into cell is: "Some Sample Text string". in this word Text should be bold and underlined remaining words normal. – Nomad Feb 11 '15 at 14:19
  • See the third code snippet in [this part of the docs](http://poi.apache.org/spreadsheet/quick-guide.html#DrawingShapes) - it's done using a [RichTextString](https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/RichTextString.html) – Gagravarr Feb 11 '15 at 16:16
  • @Gagravarr: Is poi-contrib(2.5.1-final ) comes in poi 3.11 also? – Nomad Feb 13 '15 at 17:01
  • You need to delete any POI jars that aren't from the release you're using, so if using POI 3.11 poi-contrib-2.5.1 needs to go. The contrib module was removed some time ago, so just zap the jar – Gagravarr Feb 13 '15 at 17:25

1 Answers1

0

Promoting a comment to an answer:

The method you want to use is very much present in Apache POI 3.11, and you can see full details about it in the POI Javadocs.

As detailed on the POI Components page, defining a Maven dependency on poi-ooxml will pull in the main poi component jar, which is where the IOUtils class lives, so that bit is fine

What you have in this case (based on comments) is a second, older copy of POI on your classpath. You need to remove this older POI jar (or POI jars), in common with most Java projects Apache POI will only work properly if all of the POI jars are from the same version, and no old ones are present.

Because it's a fairly common problem - lots of frameworks ship old copies of POI for example - there's a POI FAQ entry on this very thing. If you can't find the old jar by hand, you can use the code given there to work out where the old jar is to remove it.

Also, one other thing to bear in mind - many many projects provide a class called IOUtils - make sure the one you have imported into your code is org.apache.poi.util.IOUtils and not something else!

Gagravarr
  • 47,320
  • 10
  • 111
  • 156