2

I am recently using the following code to export my data in database as an excel file. But the output is showing as html page with correct data from database.

<?php
/* database connection here */

$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=$table.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");

/* extract from database */

It pulls out the all data from database so I assume database function are correct but its just not saving as excel file. This code I used exactly the same in my previous project and working fine. Any idea how to solve this? plz help.

Michiel Pater
  • 22,377
  • 5
  • 43
  • 57
zaw
  • 684
  • 1
  • 11
  • 30
  • **WARNING**: PHP 4 has been *unsupported* for over two years. You *should not* be writing new code designed for PHP 4. – Charles Apr 04 '11 at 06:21
  • His production environment might actually force him to use an old PHP version. Although I do agree with you :-). – Htbaa Apr 04 '11 at 07:23

4 Answers4

1

My advice is to inspect the generated HTTP headers and make sure they are correct. You can use Firebug's "Net" panel or the tool of your choice.

In any case, this part of your description suggests that you are not generating an Excel file:

the output is showing as html page with correct data from database

Excel files are binary files. If shown in the browser, they'll look like garbage. I suspect you are telling the browser "Here you are an Excel spreadsheet" and then sending HTML.


BTW, the Expires header does not look correct. It should contain a string with a date rather than an integer:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21

As the spec says, the browser must handle the 0 as in the past but it isn't a correctly value anyway.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Hi, Response Headers showing in firebug is Content-Type text/html; charset=UTF-8. So should I just take off expires or set something ? – zaw Apr 01 '11 at 10:26
  • Then someone else is changing the `Content-Type` header after you set it. It can be some PHP code that runs later or the web server itself (Apache, IIS or whatever). – Álvaro González Apr 01 '11 at 11:31
  • I've used [what I'm guessing is] the OP's method in the past and it works fine for a single sheet: send an Excel header and an HTML table as the content. Excel parses the HTML (including certain style info) and displays it as a worksheet. – Tim Williams Apr 01 '11 at 20:20
1

Finally just found out this is the same problem I had. Just solved it by just setting UTF-8 to UTF-8 without BOM in my editor.... code was seems correct but here is better header content I am using now. Thank you guys for help. ;)

        header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Disposition: attachment;filename=".$filename.".xls");
    header("Content-Transfer-Encoding: binary ");
zaw
  • 684
  • 1
  • 11
  • 30
0

You can use this class which lets you create excel files from PHP

PHP Class for Reading/Writing Excel Files

Another one

I recommend the first one though.

Parag Gupta
  • 117
  • 1
  • 8
  • Thanks for reply! but the requirement of using the class above is PHP version 5.2.0 or higher. Sadly php 4 don't even allow to use include(); :( – zaw Apr 01 '11 at 09:43
  • Check if the line `header("Content-Type: application/$file_type")` is parsing the variable properly. – Parag Gupta Apr 01 '11 at 09:47
  • Also, try `header("Content-Disposition: attachment; filename=\"$table.$file_ending\"");` – Parag Gupta Apr 01 '11 at 09:52
  • I also tried without using variable and put double quote. Still the same. In firebug, content-type is showing as text/html... – zaw Apr 01 '11 at 09:54
  • Also, who said PHP4 didn't have the `include()`. It does support that. – Parag Gupta Apr 01 '11 at 09:55
  • k I gonna try using class you mentioned. Include doesn't seem working earlier on my project I had OOP code which is running fine on php 5 server but when I migrate on this php 4 server those code not working at all. – zaw Apr 01 '11 at 10:11
0

There's a list of PHP libraries for reading/writing Excel files in the response to this question. I'm not sure how many of them will work with PHP4 (certainly most require PHP5), but you might find something here that will.

Community
  • 1
  • 1
Mark Baker
  • 209,507
  • 32
  • 346
  • 385