Saving images in Excel report

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi all,

I am displaying a number of reports, and giving the users an option to
display them on the web or download them to Excel. If they want the
Excel file, I just use the PHP header command to change the Content-
Type and Content-Disposition headers, and essentially print the same
report, and it's done.  That works fine.

But in some reports on the web I need to show images.  The web part is
fine, but I'm having a tough time figuring out how to actually display
the images in Excel.  Since this is a 'take it with you' file, links
won't work.  I assume I'll have to effectively store the entire real
image file within Excel in the report.  Can anyone point me in the
right direction?  Here's a code snippet:

if ( ( isset($_POST['xl'])&&($_POST['xl']=="1") ) ||
( isset($_GET['xl'])&&($_GET['xl']=="1") ) ) {
    header("Content-Type: application/xls");
    header("Content-Disposition: attachment; filename=MyReport.xls");
    header("Content-Type: application/force-download");
    header("Cache-Control: post-check=0, pre-check=0", false);
    $xl = 1;


if ($xl == 1) {
    print "<img border=0 src='../report/$file' height=20>";
else {
    print "<img border=0 src='../report/$file' height=20>";

Presently the image part doesn't work for Excel.  Thanks in advance.

Eddie Andrews

Re: Saving images in Excel report

Quoted text here. Click to load it

What do you get if you manually create an Excel spreadsheet with
images, saving it as html, then viewing in a text editor?


Re: Saving images in Excel report


Quoted text here. Click to load it

Sure, but you'd have to create your document in a native Excel format.


Re: Saving images in Excel report

Quoted text here. Click to load it

Yep. I think you're right.  Thanks.

Re: Saving images in Excel report

Eddie escribió:
Quoted text here. Click to load it

Hmmm... What's the difference?

Obviously, you aren't generating Excel files at all. This is just good
old HTML that the browser allows you to open with Excel (because of the
forged file extension) and Excel happens to understand (because of its
built-in HTML capabilities).

As you know, you don't insert pictures inside HTML but you link an
external file. So Excel only gets the HTML, there aren't any pictures.
Even if they were, according to your code it'd be looking for them in a
"report" subdirectory in the client computer.

Perhaps if you publish the pictures in your web server and provide a
full URL so Excel can download them itself _may_ do the trick (I'm not
too familiar with Excel):

<img src="hptt://">

If it doesn't, you'll need to generate a real Excel file. There're some
libraries out there.

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor al baño María:

Re: Saving images in Excel report

On Jun 12, 9:15 am, "=C1lvaro G. Vicario"
Quoted text here. Click to load it

That makes even better sense.  I'll start looking for libs.

Site Timeline