1

I am generating a Excel file in ColdFusion, using cfsavecontent. Now I want to add an image to the excel file. The code below doesn't work, resulting in a broken image.

<cfsavecontent variable="adc" >
   <cfoutput >
      <img src="output/homeicon.jpg">
   </cfoutput>
   <table>
      <tr>
        <!---  Total Row - 58  --->
        <!---  Row 1  --->
        <td style="width:64px;height:11px;background-color:#c0c0c0">
            <img src="output/homeicon.jpg">
        </td> <!---Column A--->
   </table>
<cfsavecontent>
SOS
  • 6,430
  • 2
  • 11
  • 29
  • Where the excel part? – Chris Catignani May 02 '19 at 14:05
  • 2
    That's not an Excel file. It's an *html* table that Excel happens to be able to open. That's relevant because images can't be *embedded* in an HTML file. See the [answer below](https://stackoverflow.com/a/55954446/8895292) for how to create a real Excel file, that does support embedding images. Also, while the html trick still works, it [may cause a warning due to extension hardening](https://stackoverflow.com/questions/940045/how-to-suppress-the-file-corrupt-warning-at-excel-download)). – SOS May 02 '19 at 14:56
  • 1
    @Ageax images can be embedded into HTML. See https://stackoverflow.com/questions/2807251/can-i-embed-a-png-image-into-an-html-page . Having said that, I don't think Excel would render the image for the reasons you outlined – James A Mohler May 02 '19 at 20:21
  • 1
    @JamesAMohler - True. I doubted it would work with Excel parsing html, that's why I didn't mention it. But you're right - technically it IS possible. Granted, I haven't actually tried this recently :-) – SOS May 02 '19 at 20:39

1 Answers1

4

Excel is very forgiving; writing an html table to a file, giving it an xlsx extension and telling Excel to open it will more or less work. But you can't do anything fancy, like embed an img tag and expect it work.

Coldfusion does have the capability to write proper xlsx files in the native format, and you can add an image if you do it that way. I recommend you read through https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-by-category/spreadsheet-functions.html for how to do that.

Tim Jasko
  • 1,532
  • 1
  • 8
  • 12