Excel Export using PHP

Posted on 2009-02-11
Last Modified: 2013-12-12
Hi Experts,

I want to export nested grids to excel in formatted form. Earlier, i was successful in exporting simple data.

Any idea or help will be appreciated.
Question by:Shahzad Fateh Ali
    LVL 1

    Accepted Solution


    you can write the code by yourself, but maybe you can use for your purpose.
    LVL 9

    Author Comment

    by:Shahzad Fateh Ali
    i am already trying this. Any other script?
    LVL 1

    Expert Comment

    There is simpleXml. I am using the xmlWriter Class:

            $xml = new xmlWriter();
            $xml->startDocument('1.0', 'utf-8');
            $xml->writeRaw("<?mso-application progid=\"Excel.Sheet\"?".">\n");
            $xml->writeAttribute('xmlns', 'urn:schemas-microsoft-com:office:spreadsheet');
            $xml->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
            $xml->writeAttribute('xmlns:x', 'urn:schemas-microsoft-com:office:excel');
            $xml->writeAttribute('xmlns:ss', 'urn:schemas-microsoft-com:office:spreadsheet');
            /* set document properties                   */
            $xml->writeAttribute('xmlns', 'urn:schemas-microsoft-com:office:office');
                $xml->writeElement('Author',        'My Name');
                $xml->writeElement('LastAuthor',    'My Name');
                $xml->writeElement('Created',       date('Y-m-d\Th:i:s\Z'));
                $xml->writeElement('Company',       'My Name');
            $xml->endElement();// end DocumentProperties
            /* set workbook properties                   */
            $xml->writeAttribute('xmlns', 'urn:schemas-microsoft-com:office:excel');
                $xml->writeElement('WindowHeight',      13225);
                $xml->writeElement('WindowWidth',       19382);
                $xml->writeElement('WindowTopX',        10);
                $xml->writeElement('WindowTopY',        10);
                $xml->writeElement('ProtectStructure',  'False');
                $xml->writeElement('ProtectWindows',    'False');
            $xml->endElement();// end ExcelWorkbook
            /* create worksheet                             */
            // start Worksheet
            $xml->writeAttribute('ss:Name', 'KFZ');
            // start Table
            $xml->writeAttribute('ss:ExpandedColumnCount',  (8));
            $xml->writeAttribute('ss:ExpandedRowCount',     ($rowNum));
            $xml->writeAttribute('x:FullColumns',           1);
            $xml->writeAttribute('x:FullRows',              1);
            $xml->writeAttribute('ss:DefaultColumnWidth',   106.206896551724142);
            $xml->writeAttribute('ss:DefaultRowHeight',     13.241379310344827);
            // start Title Row
            foreach ($fieldNames as $value) {
                //$xml->writeAttribute('ss:StyleID',  'Headline');
                    $xml->writeAttribute('ss:Type', 'String');
                    $xml->endElement();// end Data
                $xml->endElement();// end Cell
            $xml->endElement();// end Row
            $xml->endElement();// end Table
            $xml->endElement();// end Worksheet
            $xml->endElement();// end Workbook
            header('Content-type: application/');
            header('Content-Disposition: attachment; filename="Teilnehmerliste_'.urlencode($thisEvent[0]['TITLE']).'.xls"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0,pre-check=0');
            header('Pragma: public');
            echo $xml->outputMemory();

    Everything is possible with this method. If I don't know how which xml tags to use I save an excel file as xml, look at the code an implement it in my php script. There is no debbuging tool that tells you the line of a syntax error (like JavaScript without Firebug)
    LVL 9

    Expert Comment

    This simple class is my weapon of choice for this purpose, check out the examples:
    LVL 9

    Author Closing Comment

    by:Shahzad Fateh Ali
    Your solution worked for me.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction (All good things must come to an end ( The original MySQL API ( has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
    Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now