Export to Excel File

This piece of simple code doesn't seem to work any longer since installing a new server.

if( $_GET['action'] == 'excel') {
      header("Content-type: application/octet-stream");
      header("Content-Disposition: attachment; filename=spreadsheet.xls");
}

I'm trying to simply export data to an excel spreadsheet. This should give the option to download the file.

Thanks!
deharvyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
Try:
header("Content-type: application/vnd.ms-excel");

0
deharvyAuthor Commented:
I tried the suggested. it did not work. Any other suggestions?
0
hieloCommented:
Are you positive you are passing the correct value via the correct method? I would expect either of these two to work as long as there are no spaces preceeding or trailing the value "excel":
if( strtolower($_REQUEST['action']) == 'excel') {
      header("Content-type: application/octet-stream");
      header("Content-Disposition: attachment; filename=spreadsheet.xls");
}

if( strtolower($_REQUEST['action']) == 'excel') {
      header("Content-type: application/vnd.ms-excel");
      header("Content-Disposition: attachment; filename=spreadsheet.xls");
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

ray-solomonCommented:
Here is part of my code that I use on a site for clients to download in csv or xls format.
The csv or xls data is formatted in the $contents variable and the $filename variable is just 'whatever.csv' or 'whatever.xls'.
These headers work well with both csv and xls.

// Create some http headers
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/octet-stream"); // may also try 'application/force-download'
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Length: ".strlen($contents));
echo stripslashes($contents);
0
deharvyAuthor Commented:
Thanks for all your help. Instead of using if( strtolower($_REQUEST['action']) == 'excel') to indicate equals excel, how do I say either startsWith or includes excel? Maybe that will resolve the issue for me.
0
hieloCommented:
if( FALSE !== stristr( strtolower($_REQUEST['action']),'excel') )
{
header("Content-type: application/octet-stream");
      header("Content-Disposition: attachment; filename=spreadsheet.xls");

}
0
deharvyAuthor Commented:
Ok. Thanks! It's still not working and I'm not sure why. My link is: test.php?action=excel&name=sample

I'm sure the suggested codes above are accurate; I'm probably just doing something strange.
0
deharvyAuthor Commented:
I was able to test to validate that the action 'excel' is being met. I did so by doing:

if( $_GET['action'] == 'excel') {
 echo "test";
}

So, what else could be wrong? I tried on a different server and it works. Is there something with my PHP version?

Here is the version: PHP Version 4.4.7
0
ray-solomonCommented:
Add this code to the top of your script to see if there are any errors in your code.

error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_startup_errors','1');
ini_set('display_errors','1');
0
hieloCommented:
Try:
Content-Type

instead of:
Content-type
0
deharvyAuthor Commented:
Heilo: I'm using "Content-Type". It doesn't resolve the issue.

Ray: I used your debugging code and I think it helps. I get the following:

Notice: Undefined index: action in /home/www/test.php on line 172

------------------------

Starting at line 172.......

if( $_GET['action'] == 'excel') {
      header("Pragma: public"); // required
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: private",false); // required for certain browsers
      header("Content-Type: application/force-download");
      header("Content-Disposition: attachment; filename=\"".$filename."\";" );
      header("Content-Length: ".strlen($contents));
      echo stripslashes($contents);
      }
0
hieloCommented:
>>Undefined index: action
that means that the key 'action' in $_GET['action'] does not exist. Make sure you are supplying the correct key on the url. Example if you are sendig the user to:
site.com/download.php? action=excel
then the problem is that the "key" is not "action" but rather " action". Notice the leading space. Also makesure you are not supplying ACTION or Action. Provide the correct case as well.
0
ray-solomonCommented:
hielo is correct.

You just simply need to check if the $_GET['action'] is set.

if ((isset($_GET['action'])) && ($_GET['action'] == 'excel'))
{
      // put your header code here
}
0
deharvyAuthor Commented:
Heilo: Thanks for the explanation. My link is: http://website/test.php?action=excel&name=sample

Ray: Thanks for the code. I used it and it removed the startup index error. However, it doesn't export to excel. I'm going to create a simple page on my website and see if that works. I'll get back to you.
0
ray-solomonCommented:
You should post your entire code so we can see where the problem is.
For us, it would be very simple to figure out if we could see it.
0
hieloCommented:
See if this helps you determine the values seen by the script:
if( isset($_REQUEST['action']) && ($_REQUEST['action'] == 'excel') ) {
      //header("Content-Type: application/octet-stream");
      header("Content-Type: application/vnd.ms-excel");

      header("Content-Disposition: attachment; filename=spreadsheet.xls");

      }
       else
       {
             echo "isset: " . isset($_REQUEST['action']);
            echo "action: " . $_REQUEST['action'];
}

Just so we are clear, this line:
header("Content-Disposition: attachment; filename=spreadsheet.xls");

DOES NOT send the spreadsheet.xsl file to the browser. It merely tells the browser, it is about to receive a file named spreadsheet.xls

To actually send the contents of the file, you would need to open the file (look up fopen), read its  contents, add a Content-Lenght header, and lastly, echo/print the contents of the file. If you do not echo/print the contents of the file, the browser will not receive anything.
0
deharvyAuthor Commented:
At the top of the page, I see this:

isset: action:

I guess it's not finding the first condition.
0
hieloCommented:
>>I guess it's not finding the first condition.
Correct.

If you actually provided a querystring, like this:
http://website/test.php?action=hi&name=sample

Then 'action' should report something in the else. It does not have to have excel as its value.
0
deharvyAuthor Commented:
Oddly enough, I used this code:
if( isset($_GET['action']) && ($_GET['action'] !== 'excel') ) {

When I did this, I got this output:
isset: 1action: excel

This indicates to me that the proper values are being met. That's frustrating.

(I know I've taken up alot of your time. Let me know when it's time to move on)
0
hieloCommented:
Based on your previous post, we are getting somewhere. Let's cast the $_GET to a string and see if it helps:
if( isset($_REQUEST['action']) && ( strval($_REQUEST['action']) === 'excel') ) {
        echo "ready to send file!"
      }
       else
       {
             echo "isset: " . isset($_REQUEST['action']);
            echo "action: " . $_REQUEST['action'];
}

Open in new window

0
deharvyAuthor Commented:
It definitely outputs "ready to send file!"

However, I added:

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=spreadsheet.xls");

Unfortunately, it still doesn't export to a file. ;(
0
hieloCommented:
>>It definitely outputs "ready to send file!"
OK. This is great news.

Simply adding:
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=spreadsheet.xls");

will not cause the server to send the file to the client. You need to actually "send" something. I explained this issue above (http:#20780383). On the code below I am "sending" the contnent by simply echoing the file contents. Try the code below.
if( isset($_REQUEST['action']) && ( strval($_REQUEST['action']) === 'excel') )
{
	header("Content-Type: application/vnd.ms-excel");
	header("Content-Disposition: attachment; filename=spreadsheet.xls");
	echo('<table><tr><th>State</th><th>Name</th></tr><tr><td>CA</td><td>California</td></tr><tr><td>NY</td><td>New York</td></tr></table>');
}
else
{
	echo "isset: " . isset($_REQUEST['action']);
	echo "action: " . $_REQUEST['action'];
}

Open in new window

0
deharvyAuthor Commented:
The page echos:

State      Name
CA      California
NY      New York

But not in a file, just a web page. :(
0
hieloCommented:
Did you copy and paste from my post above. I just tested it and it works perfectly for me.
0
deharvyAuthor Commented:
Heilo: I really don't think the issue has to do with the code. I think there is something with the PHP version.

On a different server that uses a different version of PHP, the initial code works (as does your code above).

I'm trying to migrate to a new host that uses PHP 4.4.7. There has to be something there. What version of PHP are you testing with?
0
hieloCommented:
I have PHP 5.2.1
BTW, look at the example by "lasitha dot alawatta at gmail dot com" on "29-Jul-2007 01:15" at http://us.php.net/header. Also, read the one right above it. Not sure if you are using IE7.
0
deharvyAuthor Commented:
Heilo, first off, you have been great.

Just creating a simple page, it works:

<?php
      header("Content-Type: application/vnd.ms-excel");
      header("Content-Disposition: attachment; filename=spreadsheet.xls");
      echo('<table><tr><th>State</th><th>Name</th></tr><tr><td>CA</td><td>California</td></tr><tr><td>NY</td><td>New York</td></tr></table>');
?>

There is definitely something in my full code that's messing things up.
0
deharvyAuthor Commented:
I think I made some progress:

Warning: Cannot modify header information - headers already sent by....

That's when I put the code in debugging mode. Any ideas?
0
deharvyAuthor Commented:
Warning: Cannot modify header information - headers already sent by (output started at /homepages/website/test.php:1) in /homepages/website/test.php on line 173
Warning: Cannot modify header information - headers already sent by (output started at /homepages/website/test.php:1) in /homepages/website/test.php on line 174

The web says it's an infamous "whitespace" issue.

My first few lines are at the bottom

-------------------------------

<?php
///*
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_startup_errors','1');
ini_set('display_errors','1');
//*/
0
deharvyAuthor Commented:
Thanks for all your help!
0
deharvyAuthor Commented:
All along, it appears the problem was the whitespace on the very first line of my code. I simply placed the file on a Unix server, used 'vi' to edit it and removed the junk.

I learned many troubleshooting routines in this post that will be valuable well into the future. Error checking, Manipulating header output, echoing to validate values... all important stuff.

Thanks to all (especially heilo) for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.