home | contact us
» excel » PHP : Dead Easy Excel Export

BLOG CATEGORY: excel php


Some people like their excel files. For people who want their data exported in an excel format checkout this chunk of code. It’s really easy :-)

First of all you need to download this php excel class

Now try this code:

$query = mysql_query("select * from table");
while($q = mysql_fetch_assoc($query)){
	$output[] = $q;
}
require_once "excel.php";
$export_file = "xlsfile://export.xls";
$fp = fopen($export_file, "wb");
if (!is_resource($fp))
{
    die("Cannot open $export_file");
}
fwrite($fp, serialize($output));
fclose($fp);
header ("Content-Type: application/x-msexcel");
header ("Content-Disposition: attachment; filename=\"exports.xls\"" );
readfile("xlsfile://export.xls");
exit;

That’s got to be the easiest thing you have ever done in PHP. Thanks goes to the excellent PHP Classes site.

Here is some further reading:
PHP Excel Formula Parser
PHP Excel Export class
Export data directly to Excel by configuring the MIME Type Profile Option
Power your PHP Business Logic with Excel
Reading/Parsing Excel Spreadsheet using PHP


Click Here to Contact Us about PHP : Dead Easy Excel Export
 

Comments

5 Comments

5 Responses to “PHP : Dead Easy Excel Export”

  1. admin says:

    Note – on a unix/linux host this code might not work

    to fix this try the following.

    line 71 of the excel class

     function stream_open($path, $mode, $options, &$opened_path)
        {
        	/* lets do it quick and dirty
            $url = parse_url($path);
            $this->xlsfilename = '/' . $url['host'] . $url['path'];
            $this->xlsfilename = $url['host'] . $url['path'];
            */
            $this->xlsfilename = str_replace('xlsfile://', '', $path);
    
  2. Dave Gee says:

    Is my website outdated for today’s programmers demands?

  3. Ran says:

    Thank you verymuch. And thanks to admin for the update!!!

  4. ebi says:

    how can i download this package ? help me

  5. “PHP : Dead Easy Excel Export – Edmonds Commerce Magento Silver
    Partner” was indeed a great blog, cannot
    help but wait to read through even more of ur blog posts.
    Time to spend several time on the internet haha.
    Thank you ,Keisha

Leave a Reply

rss icon