Skip to main content
Participant
July 31, 2008
Question

Inserting an Image in Excel with POI HSSF

  • July 31, 2008
  • 1 reply
  • 3675 views
Has anyone been successful creating an Excel document with an embedded image using POI HSSF?

Create a nicely formatted Excel is no, problem - but I can't seem to figure the image part out. I am not calling the loadPicture correctly, which is confusing CF.

<cfset patriarch = newSheet.createDrawingPatriarch()/>
<cfset patriarch.createPicture(createObject("java",
"org.apache.poi.hssf.usermodel.HSSFClientAnchor").init(),
loadPicture("asset/images/logo_e2eA.png", workSheet))/>
This topic has been closed for replies.

1 reply

Inspiring
August 1, 2008
MinOP wrote:
> but I can't seem to figure the image part out. I am not calling the loadPicture correctly, which is confusing CF.

You did not mention which version of POI. Did you verify your version supports the patriarch.createPicture method? If you are using a really old version it might not.

> loadPicture("asset/images/logo_e2eA.png", workSheet))/>

Does the code actually have access to a loadPicture() method or function? I think loadPicture() is a private method in one of the example classes: org.apache.poi.hssf.usermodel.examples.*. So you may have to write your own version of it. Assuming you have not done so already...
Inspiring
August 1, 2008
MinOP,

I found the source code for the loadPicture method in svn and adapted it to CF. Here is a working example. It creates a new workbook, adds an image, and saves the file to disk.

A few notes about the code snippet. The code uses the JavaLoader.cfc
JavaLoader.cfc to load a newer version of the POI jar. The loadPicture function assumes the image is .png format. Adapt it as needed.





Inspiring
August 3, 2008
MinOP,

I tweaked the code and here is a simplified version. The same notes apply: it requires the JavaLoader.cfc and assumes the image is .png format.