php spreadsheet

Download Php Spreadsheet

Post on 01-Nov-2014

34 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Php Spreadsheet

TRANSCRIPT

1 evZone IeadIn and WrItIn Spreadsheets wIth III07/02/2012 05.18.43 IM http.//devzone.zend.com/27/readIn-and-wrItIn-spreadsheets-wIth-php/* Link http://devone.end .com* PEAR * Link http://devone. end.com/tag/pear/*Reading and Writing Spreadseets wit PHPVikram Vaswani * Link http://devone .end. com/author/Vikram Vaswani/* j 16 comments * Link ttp://devz one.z end. com/27/reading-and-writing- spreadseets- wit- pp/#comments* j November 30th, -0001Breaking Te Language BarrierWhen it comes to playing nice with data in different formats, PHP's pedigree is hardto beat. Not only does the language make it a breee todeal with SQL resultsets andXML files, but it comes with extensions to deal with formats as diverse as Ogg/Vorbis audio files, ZIP archives andEXIF headers. So it shouldcome as no surprise that PHP can also readand write Microsoft Excel spreadsheets, albeit with a little help fromPEAR.In this article , I'll introduce you to two packages that make it surprisingly easy to hook your PHP scripts up to a Microsoft Excel spreadsheet andextract the data contained therein. I'll also show you how to dynamically create a new spreadsheet from scratch, complete with formulae andformatting, and import data from a spreadsheet into a database. So come on it, and let's get started !Putting Te Pieces TogeterThere isn't currently a unifiedreader/writer package for Excel spreadsheets in PEAR, so this tutorial will make use of two separate packages:The PHP-ExcelReader package , currently not maintained but available at http://sourceforge. net/projects/phpexcelreader/ * Link http://sourceforge.net/projects/phpexcelreader/*The Spreadsheet_Excel_Writer package, maintained by Xavier Noguer and available from http://pear.php.net/package /Spreadsheet_Excel_Writer * Link http://pear. php.net/package/Spreadsheet_Excel_Writer*For the latter package , you'll also needto downloadand install the PEAR OLE package , from http://pear.php.net/package /OLE * Link http://pear. php.net/package/OLE*You can install these packages manually, or using the PEAR installer, as below:she> peaJ 1hs1a SpJeashee1_Exce_WJ11eJI shouldmention at this point certain changes you might need to make to your development environment in order to get the PHP-ExcelReaderpackage working. As noted above, this package is currently not maintained andso, simply include ()-ing the main class file, 'reader.php', in yourscript, as suggestedin the package documentation, is insufficient and generates a 'missing file' error.To get things working, extract the file 'oleread .inc' from the distribution archive andsave it to a location in your PHP include path using thedirectory structure 'Spreadsheet/Excel/Reader/OLERead .php'. Once you do this, the error shoulddisappear and things shouldbegin workingnormally. There's a caveat, though: this procedure works with the version of PHP-ExcelReader datedJul 3 2007 (the most current version at thetime of writing) but may not work with other versions.Te Number GameNow that you have all the pieces installed, let's see them in action. Assume for a moment that you have the following Excel spreadsheet:2 evZone IeadIn and WrItIn Spreadsheets wIth III07/02/2012 05.18.43 IM http.//devzone.zend.com/27/readIn-and-wrItIn-spreadsheets-wIth-php/Now, let's put together a simple PHP script that reads this spreadsheet anddisplays the number of worksheets, rows and columns in it:Jea{`Book1.xs`),// pJ1h1 humbeJ o1 Jows, coumhs ah shee1secho "humbeJ o1 shee1s. " . s1zeo1{$exce->shee1s) . "\h",1oJ {$x=0, $xshee1s), $x++) {echo "humbeJ o1 Jows 1h shee1 " . {$x+1) . ". " . $exce->shee1s|$x]|"humRows"] . "\h",echo "humbeJ o1 coumhs 1h shee1 " . {$x+1) . ". " . $exce->shee1s|$x]|"humCos"] . "\h",}?>Here's the output:3 evZone IeadIn and WrItIn Spreadsheets wIth III07/02/2012 05.18.43 IM http.//devzone.zend.com/27/readIn-and-wrItIn-spreadsheets-wIth-php/How didthis happen? It's actually not very difficult, because PHP-ExcelReader does most of the heavy lifting for you. The first step is to includethe package file andinstantiate a new Spreadsheet_Excel_Reader object. This object exposes a read() method , which accepts the name andpath to the source Excel spreadsheet as input argument andthen goes to work reading the spreadsheet data andconverting it into PHP-readabledata structures.Every Excel spreadsheet file contains one or more worksheets, these worksheets are now representedin the Spreadsheet_Excel_Reader's'sheets' property, as array elements indexed from 0. As a result, worksheet 1 may be accessedusing the notation $obj-sheets[0|, worksheet 2as $obj-sheets[1|, andso on.For each worksheet, PHP-ExcelReader also creates some special array keys with sheet-level informationfor example , the 'numRows' key holdsthe number of rows in the worksheet, while the 'numCols' holds the number of columns. The script above merely uses this information to iterateover the array of worksheets andprint the number of rows andcolumns in each.Off Te GridNow, while this is interesting, it's not really all that usefulafter all, what you're really after is the data within the spreadsheet. PHP-ExcelReaderhas you coveredthere too: the cells of each worksheet are nestedinside the corresponding worksheet array, andare accessible via the 'cells'array key. An individual cell is accessedusing its row and column coordinatesfor example , the cell at row 3 and column C would beaccessed using the notation $obj-sheets[0|[' cells'|[3|[3|. Row and column indexing starts from 1.With this in mind , consider the next PHP script, which uses the PHP-ExcelReader to represent the example spreadsheet as an HTML table:

1abe { boJeJ-coapse. coapse,}1 { boJeJ. 1px so1 back, pa1hg. 0 0.5em,}