1、 PHPExcel Developer Documentation 1. Contents PHPExcel Developer Documentation 1 1. Contents 2 2. Prerequisites 4 2.1. Software requirements 4 2.2. Installation instructions 4 2.3. Getting started 4 2.4. Useful links and to
2、ols 4 2.4.1. OpenXML / SpreadsheetML 4 2.4.2. Frequently asked questions 5 2.4.3. Tutorials 5 3. Architecture 6 3.1. Schematical 6 3.2. Lazy Loader 6 3.3. Spreadsheet in memory 6 3.4. Readers and writers 6 3.5. Fluent interfaces 7 4. Creating a spreadsheet 9 4.1. The PHPExcel class 9 4.2
3、 Configuration Settings 9 4.2.1. Cell Caching 9 4.2.2. Language/Locale 11 4.3. Clearing a Workbook from memory 11 4.4. Worksheets 11 4.5. Accessing cells 11 4.5.1. Setting a cell value by coordinate 11 4.5.2. Retrieving a cell by coordinate 11 4.5.3. Setting a cell value by column and row 1
4、1 4.5.4. Retrieving a cell by column and row 12 4.5.5. Looping cells 12 4.5.6. Using value binders to facilitate data entry 13 4.6. PHPExcel recipes 14 4.6.1. Setting a spreadsheet’s metadata 14 4.6.2. Setting a spreadsheet’s active sheet 14 4.6.3. Write a date into a cell 14 4.6.4. Write a
5、formula into a cell 15 4.6.5. Locale Settings for Formulae 15 4.6.6. Write a newline character "\n" in a cell (ALT+"Enter") 16 4.6.7. Explicitly set a cell’s datatype 17 4.6.8. Change a cell into a clickable URL 17 4.6.9. Setting a worksheet’s page orientation and size 17 4.6.10. Page Setup: S
6、caling options 17 4.6.11. Page margins 18 4.6.12. Center a page horizontally/vertically 19 4.6.13. Setting the print header and footer of a worksheet 19 4.6.14. Setting printing breaks on a row or column 20 4.6.15. Show/hide gridlines when printing 20 4.6.16. Setting rows/columns to repeat at
7、top/left 21 4.6.17. Specify printing area 21 4.6.18. Formatting cells 21 4.6.19. Number formats 22 4.6.20. Alignment and wrap text 23 4.6.21. Setting the default style of a workbook 23 4.6.22. Styling cell borders 23 4.6.23. Conditional formatting a cell 24 4.6.24. Add a comment to a cell 25
8、 4.6.25. Apply autofilter to a range of cells 25 4.6.26. Setting security on a spreadsheet 25 4.6.27. Setting data validation on a cell 26 4.6.28. Setting a column’s width 27 4.6.29. Show/hide a column 27 4.6.30. Group/outline a column 27 4.6.31. Setting a row’s height 28 4.6.32. Show/hide a
9、 row 28 4.6.33. Group/outline a row 28 4.6.34. Merge/unmerge cells 28 4.6.35. Inserting rows/columns 28 4.6.36. Add a drawing to a worksheet 29 4.6.37. Add rich text to a cell 29 4.6.38. Define a named range 29 4.6.39. Redirect output to a client’s web browser 30 4.6.40. Setting the default
10、column width 31 4.6.41. Setting the default row height 31 4.6.42. Add a GD drawing to a worksheet 31 4.6.43. Setting worksheet zoom level 31 4.6.44. Sheet tab color 31 4.6.45. Creating worksheets in a workbook 31 4.6.46. Hidden worksheets (Sheet states) 31 4.6.47. Right-to-left worksheet 32
11、5. Performing formula calculations 33 5.1. Using the PHPExcel calculation engine 33 5.2. Known limitations 34 5.2.1. Operator precedence 34 5.2.2. Formulas involving numbers and text 34 6. Reading and writing to file 35 6.1. PHPExcel_IOFactory 35 6.1.1. Creating PHPExcel_Reader_IReader using
12、PHPExcel_IOFactory 35 6.1.2. Creating PHPExcel_Writer_IWriter using PHPExcel_IOFactory 35 6.2. Excel 2007 (SpreadsheetML) file format 35 6.2.1. PHPExcel_Reader_Excel2007 36 6.2.2. PHPExcel_Writer_Excel2007 36 6.3. Serialized file format 37 6.3.1. PHPExcel_Reader_Serialized 37 6.3.2. PHPExcel_
13、Writer_Serialized 37 6.4. Excel 5 (BIFF) file format 37 6.4.1. PHPExcel_Reader_Excel5 37 6.4.2. PHPExcel_Writer_Excel5 38 6.5. Excel 2003 XML file format 38 6.5.1. PHPExcel_Reader_Excel2003XML 38 6.6. Symbolic LinK (SYLK) 39 6.6.1. PHPExcel_Reader_SYLK 39 6.7. CSV (Comma Separated Values) 40
14、 6.7.1. PHPExcel_Reader_CSV 40 6.7.2. PHPExcel_Writer_CSV 40 6.8. HTML 41 6.8.1. PHPExcel_Writer_HTML 41 6.9. PDF 43 6.9.1. PHPExcel_Writer_PDF 43 6.10. Generating Excel files from templates (read, modify, write) 44 7. Credits 45 Appendix A: Valid array keys for style applyFromArray() 46
15、 2. Prerequisites 2.1. Software requirements The following software is required to develop using PHPExcel: » PHP version 5.2.0 or newer » PHP extension php_zip enabled *) » PHP extension php_xml enabled » PHP extension php_gd2 enabled (if not compiled in) *) php_zip is only needed by PHPExc
16、el_Reader_Excel2007, PHPExcel_Writer_Excel2007 and PHPExcel_Reader_OOCalc. In other words, if you need PHPExcel to handle .xlsx or .ods files you will need the zip extension, but otherwise not. 2.2. Installation instructions Installation is quite easy: copy the contents of the Classes folder to an
17、y location in your application required. Example: If your web root folder is /var/www/ you may want to create a subfolder called /var/www/Classes/ and copy the files into that folder so you end up with files: /var/www/Classes/PHPExcel.php /var/www/Classes/PHPExcel/Calculation.php /var/www/
18、Classes/PHPExcel/Cell.php ... 2.3. Getting started A good way to get started is to run some of the tests included in the download. Copy the "Tests" folder next to your "Classes" folder from above so you end up with: /var/www/Tests/01simple.php /var/www/Tests/02types.php ... Start running
19、 the tests by pointing your browser to the test scripts: ... Note: It may be necessary to modify the include/require statements at the beginning of each of the test scripts if your "Classes" folder from above is named differently. 2.4. Useful links and tools There are some links and tool
20、s which are very useful when developing using PHPExcel. Please refer to the PHPExcel CodePlex pages for an update version of the list below. 2.4.1. OpenXML / SpreadsheetML » File format documentation http://www.ecma-international.org/news/TC45_current_work/TC45_available_docs.htm » OpenXML Exp
21、lained e-book http://openxmldeveloper.org/articles/1970.aspx » Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats » OpenXML Package Explorer 2.4.2. Frequently asked questions The up-to-date F.A.Q. page for PHPExcel can be found on There seems to be
22、a problem with character encoding... It is necessary to use UTF-8 encoding for all texts in PHPExcel. If the script uses different encoding then it is possible to convert the texts with PHP's iconv() function. PHP complains about ZipArchive not being found Make sure you meet all requirements, esp
23、ecially php_zip extension should be enabled. Excel 2007 cannot open the file generated by PHPExcel_Writer_2007 on Windows “Excel found unreadable content in '*.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.” Some versions of the
24、 php_zip extension on Windows contain an error when creating ZIP files. The version that can be found on should work at all times. Protection on my worksheet is not working? When you make use of any of the worksheet protection features (e.g. cell range protection, prohibiting deleting rows, ...),
25、 make sure you enable worksheet security. This can for example be done like this: $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); Feature X is not working with PHPExcel_Writer_Y Not all features of PHPExcel are implemented in the Reader / Writer classes. This is mostly due to u
26、nderlying libraries not supporting a specific feature or not having implemented a specific feature. For example autofilter is not implemented in PEAR Spreadsheet_Excel_writer, which is the base of our Excel5 writer. Formulas don’t seem to be calculated in Excel2003 using compatibility pack? Thi
27、s is normal behaviour of the compatibility pack, Excel2007 displays this correctly. Use PHPExcel_Writer_Excel5 if you really need calculated values, or force recalculation in Excel2003. Setting column width is not 100% accurate Trying to set column width, I experience one problem. When I open the
28、file in Excel, the actual width is 0.71 less than it should be. The short answer is that PHPExcel uses a measure where padding is included. See section: “Setting a column’s width” for more details. 2.4.3. Tutorials » English PHPExcel tutorial http://openxmldeveloper » French PHPExcel tutor
29、ial http://g- » Russian PHPExcel Blog Postings http://www.web- 3. Architecture 3.1. Schematical 3.2. Lazy Loader PHPExcel implements an autoloader or “lazy loader”, which means that it is not necessary to include every file within PHPExcel. It is only necessary to include the initial PHPE
30、xcel class file, then the autoloader will include other class files as and when required, so only those files that are actually required by your script will be loaded into PHP memory. The main benefit of this is that it reduces the memory footprint of PHPExcel itself, so that it uses less PHP memory
31、 If your own scripts already define an autoload function, then this may be overwritten by the PHPExcel autoload function. For example, if you have: function __autoload($class) { ... } Do this instead: function myAutoload($class) { ... } spl_autoload_register('myAutoload');
32、 Your autoloader will then co-exist with the autoloader of PHPExcel. 3.3. Spreadsheet in memory PHPExcel’s architecture is built in a way that it can serve as an in-memory spreadsheet. This means that, if one would want to create a web based view of a spreadsheet which communicates with PHPExcel
33、’s object model, he would only have to write the front-end code. Just like desktop spreadsheet software, PHPExcel represents a spreadsheet containing one or more worksheets, which contain cells with data, formulas, images, … 3.4. Readers and writers On its own, PHPExcel does not provide the fun
34、ctionality to read from or write to a persisted spreadsheet (on disk or in a database). To provide that functionality, readers and writers can be used. By default, the PHPExcel package provides some readers and writers, including one for the Open XML spreadsheet format (a.k.a. Excel 2007 file for
35、mat). You are not limited to the default readers and writers, as you are free to implement the PHPExcel_Writer_IReader and PHPExcel_Writer_IWriter interface in a custom class. 3.5. Fluent interfaces PHPExcel supports fluent interfaces in most locations. This means that you can easily “chain” c
36、alls to specific methods without requiring a new PHP statement. For example, take the following code: $objPHPExcel->getProperties()->setCreator("Maarten Balliauw"); $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw"); $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Te
37、st Document"); $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document"); $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes."); $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php"); $objPHPExcel->getP
38、roperties()->setCategory("Test result file"); This can be rewritten as: $objPHPExcel->getProperties() ->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2
39、007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); i Using fluent interfaces is not required Fluent interfaces have bee
40、n implemented to provide a convenient programming API. Use of them is not required, but can make your code easier to read and maintain. 4. Creating a spreadsheet 4.1. The PHPExcel class The PHPExcel class is the core of PHPExcel. It contains references to the contained worksheets, document secu
41、rity settings and document meta data. To simplify the PHPExcel concept: the PHPExcel class represents your workbook. 4.2. Configuration Settings Once you have included the PHPExcel files in your script, but before instantiating a PHPExcel object or loading a workbook file, there are a number of
42、 configuration options that can be set which will affect the subsequent behaviour of the script. 4.2.1. Cell Caching PHPExcel uses an average of about 1k/cell in your worksheets, so large workbooks can quickly use up available memory. Cell caching provides a mechanism that allows PHPExcel to maint
43、ain the cell objects in a smaller size of memory, on disk, or in APC, memcache or Wincache, rather than in PHP memory. This allows you to reduce the memory usage for large workbooks, although at a cost of speed to access cell data. By default, PHPExcel still holds all cell objects in memory, but yo
44、u can specify alternatives. To enable cell caching, you must call the PHPExcel_Settings::setCacheStorageMethod() method, passing in the caching method that you wish to use. $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory; PHPExcel_Settings::setCacheStorageMethod($cacheMethod
45、); setCacheStorageMethod() will return a boolean true on success, false on failure (for example if trying to cache to APC when APC is not enabled). A separate cache is maintained for each individual worksheet, and is automatically created when the worksheet is instantiated based on the caching
46、 method and settings that you have configured. You cannot change the configuration settings once you have started to read a workbook, or have created your first worksheet. Currently, the following caching methods are available. PHPExcel_CachedObjectStorageFactory::cache_in_memory; The default
47、 If you don’t initialise any caching method, then this is the method that PHPExcel will use. Cell objects are maintained in PHP memory as at present. PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized; Using this caching method, cells are held in PHP memory as an array of serialized
48、objects, which reduces the memory footprint with minimal performance overhead. PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip; Like cache_in_memory_serialized, this method holds cells in PHP memory as an array of serialized objects, but gzipped to reduce the memory usage still further, although access to read or write a cell is slightly slower. PHPExcel_CachedObjectStorageFactory::cache_to_discISAM; When using cache_to_disc






