PHP Excel Export class

Following the code that I found here, I made a quick PHP4 class that creates a single sheet Excel file. The class has a download() function that will send the appropriate headers for a binary download.

ExcelExport.php – Rename to ExcelExport.php.

Usage

require_once "ExcelExport.php";

$xls = new ExcelExport();

$xls->addRow(Array("First Name","Last Name","Website","ID"));
$xls->addRow(Array("james","lin","www.chumby.net",0));
$xls->addRow(Array("bhaven","mistry","www.mygumballs.com",1));
$xls->addRow(Array("erica","truex","www.wholegrainfilms.com",2));
$xls->addRow(Array("eliot","gann","www.dissolvedfish.com",3));
$xls->addRow(Array("trevor","powell","gradius.classicgaming.gamespy.com",4));
$xls->download("websites.xls");

18 thoughts on “PHP Excel Export class”

  1. How can i use this script with array’s?
    I want to get all de field names from my Database and set this at the first line of my excel file?

  2. I have tested your Excel export script and it works great except one thing. It seems to have problem if the text is longer than 255 characters in the same cell. Do you know why?

  3. Hey! Nice class. Works very well. But how can I add a auto sum formula on the generated excel sheet?

  4. below is my code used to fetch values from databse but its all showing in one row not in different coloumns below is my code ,if u have better code pls post here

    $sql=”select * from login”;
    $query=mysql_query($sql);
    $fieldCount = mysql_num_fields($query);
    for ($i=0; $iaddRow(Array($fieldName));

    $valueCount = mysql_num_rows($query);
    for ($i=0; $iaddRow(Array($userval));
    }

    $xls->download(“websites.xls”);

  5. /*$sql=”select * from login”;
    $query=mysql_query($sql);
    $fieldCount = mysql_num_fields($query);
    for ($i=0; $iaddRow(Array($fieldName));

    $valueCount = mysql_num_rows($query);
    for ($i=0; $iaddRow(Array($userval));
    }

    $xls->download(“websites.xls”);*/

  6. Very nice and useful script.
    Thanks.
    I have changed a little addRow method in order to support associative array’s, I hope you don’t mind.
    Regards.

  7. Can anyone improve on this function to take any sql statement and output the xls. This does work… just need to know i’m not missing the obvious.

    <?php

    $filename=”top_selling”;

    $select = ”

    SELECT DISTINCT
    product_category.category,
    product_category.category_description,
    product_family.name,
    cust_order_lines.order_line_sku,
    Count(cust_order_lines.order_line_sku) AS qty
    FROM
    cust_order_lines ,
    product_family
    Inner Join product_sku ON cust_order_lines.order_line_sku = product_sku.sku_id AND product_sku.pf_id = product_family.pf_id
    Inner Join product_category ON product_family.cat_id = product_category.category
    GROUP BY
    product_category.category,
    product_category.category_description,
    product_family.name
    ORDER BY
    qty DESC

    “;
    export_excel_report($select,$filename);

    function export_excel_report($select,$filename){
    require_once “ExcelExport.php”;
    include(“../functions/dbconn.php”);
    $xls = new ExcelExport();
    $export = mysql_query($select);
    $count = mysql_num_fields($export);
    $j=0; $k=0;
    for ($i = 0; $i < $count; $i++) {
    $headername[$i] = mysql_field_name($export, $i);
    }
    for ($i = 0; $i writeCell($headername[$i],$j,$k++);
    }

    $j=1; $k=0;
    while($row = mysql_fetch_row($export)) {
    for ($i = 0; $i writeCell($row[$i],$j,$k++);
    }
    $j++;$k=0;
    }
    $xls->download($filename.”.xls”);
    }
    ?>

Leave a Reply