Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Azure, PHP, OpenXML, VSTS, ...

About the author

Maarten Balliauw is an MVP ASP.NET and is currently employed as .NET Software Engineer at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP.
More about me More about me
Send mail E-mail me


Microsoft Most Valuable Professional - MVP - ASP.NET

Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn RealDolmen - Rock-solid passion for ICT
I'm a speaker at TechDays Belgium and TechDays Finland

Search

Latest Twitter

    Follow me on Twitter...

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2010

    Saving a PHPExcel spreadsheet to Google Documents

    As you may know, PHPExcel is built using an extensible model, supporting different input and output formats. The PHPExcel core class library features a spreadsheet engine, which is supported by IReader and IWriter instances used for reading and writing a spreadsheet to/from a file.

    PHPExcel architecture

    Currently, PHPExcel supports writers for Excel2007, Excel5 (Excel 97+), CSV, HTML and PDF. Wouldnt it be nice if we could use PHPExcel to store a spreadsheet on Google Documents? Let’s combine some technologies:

    Creating a custom GoogleDocs writer

    First, we need an implementation of PHPExcel_Writer_IWriter which will support writing stuff to Google Documents. Since Google accepts XLS files and Zend_Gdata provides an upload method, I think an overloaded version of PHPExcel’s integrated PHPExcel_Writer_Excel5 will be a good starting point.

    class PHPExcel_Writer_GoogleDocs extends PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter {
            // ...
    }

    Since Google requires to log in prior to being able to interact with the documents stored on Google Documents, let’s also add a username and password field.

    class PHPExcel_Writer_GoogleDocs extends PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter {
        private $_username;
        private $_password;

        public function setCredentials($username, $password) {
            $this->_username = $username;
            $this->_password = $password;
        }
    }

    Next, let’s override the save() method. This method will save the document as an XLS spreadsheet somewhere, upload it to Google Docs and afterwards remove it from the file system. Here we go:

    public function save($pFilename = null) {
            parent::save($pFilename);
            $googleDocsClient = Zend_Gdata_ClientLogin::getHttpClient($this->_username,
                    $this->_password, Zend_Gdata_Docs::AUTH_SERVICE_NAME);
            $googleDocsService = new Zend_Gdata_Docs($googleDocsClient);
            $googleDocsService->uploadFile($pFilename, basename($pFilename), null,
                    Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI);

            @unlink($pFilename);
    }

    Nothing more! This should be our new writer class.

    Using the GoogleDocs writer

    Now let’s try saving a spreadsheet to Google Docs. First of all, we load a document we have stored somewhere on the file system:

    $objReader = PHPExcel_IOFactory::createReader('Excel2007');
    $objPHPExcel = $objReader->load("05featuredemo.xlsx");

    Next, let’s use PHPExcel’s IOFactory class to load our PHPExcel_Writer_GoogleDocs class. We will also set credentials on it. Afterwards, we save.

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'GoogleDocs');
    $objWriter->setCredentials('xxxxxxxx@gmail.com', 'xxxxxxxx');
    $objWriter->save('somefile.xls');

    This should be all there is to it. Google Docs will now contain our spreadsheet created using PHPExcel.

    Google Docs Image

    Note that images are not displayed due to the fact that Google Docs seems to remove them when uploading a document. But hey, it’s a start!

    You can download the full example code here (26.29 kb). Make sure you have PHPExcel, Zend Framework and Zend Gdata classes installed on your system.


    Comments

    Microsoft RealDolmen blogs | Reply

    Tuesday, February 03, 2009 12:33 PM

    trackback

    Trackback from Microsoft RealDolmen blogs

    Saving a PHPExcel spreadsheet to Google Documents

    alvinashcraft.com | Reply

    Wednesday, February 04, 2009 5:58 PM

    pingback

    Pingback from alvinashcraft.com

    Dew Drop - February 4, 2009 | Alvin Ashcraft's Morning Dew

    Julien Chable | Reply

    Friday, February 06, 2009 11:40 AM

    trackback

    Trackback from Julien Chable

    [Open XML] Liens de la semaine 02/02/09

    Doug Mahugh | Reply

    Monday, March 02, 2009 7:32 AM

    trackback

    Trackback from Doug Mahugh

    Odds & ends for 03-01-2009

    clauswitt.com | Reply

    Sunday, March 08, 2009 11:03 AM

    pingback

    Pingback from clauswitt.com

    PHPExcel and Google Docs Together | clauswitt.com

    answerspluto.com | Reply

    Tuesday, July 14, 2009 4:28 AM

    pingback

    Pingback from answerspluto.com

    list of urls - 5 « Answers Pluto

    oshuamoreno.com | Reply

    Friday, October 23, 2009 2:32 AM

    pingback

    Pingback from oshuamoreno.com

    PHPExcel: read Excel from LAMP | Oshua Moreno

    Add comment




      Country flag

    biuquote
    • Comment
    • Preview
    Loading