Overview

Namespaces

  • PHP
  • SimpleExcel
    • Enums
    • Parser
    • Spreadsheet
    • Writer

Classes

  • BaseWriter
  • CSVWriter
  • HTMLWriter
  • JSONWriter
  • TSVWriter
  • XLSXWriter
  • XMLWriter

Interfaces

  • IWriter
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace SimpleExcel\Writer;
  4: 
  5: use SimpleExcel\Enums\Datatype;
  6: 
  7: /**
  8:  * SimpleExcel class for writing Microsoft Excel 2003 XML Spreadsheet
  9:  *  
 10:  * @author  Faisalman
 11:  * @package SimpleExcel
 12:  */
 13: class XMLWriter extends BaseWriter
 14: {
 15:     /**
 16:      * Defines content-type for HTTP header
 17:      * 
 18:      * @access  protected
 19:      * @var     string
 20:      */
 21:     protected $content_type = 'application/xml';
 22: 
 23:     /**
 24:      * Defines file extension to be used when saving file
 25:      * 
 26:      * @access  protected
 27:      * @var     string
 28:      */
 29:     protected $file_extension = 'xml';
 30: 
 31:     /**
 32:      * Array containing document properties
 33:      * 
 34:      * @access  private
 35:      * @var     array
 36:      */
 37:     private $doc_prop;
 38: 
 39:     /**
 40:      * @param   Workbook    reference to workbook
 41:      * @return  void
 42:      */
 43:     public function __construct(&$workbook){
 44:         parent::__construct(&$workbook);
 45:         $this->doc_prop = array(
 46:             'Author' => 'SimpleExcel.php',
 47:             'Company' => 'SimpleExcel.php',
 48:             'Created' => gmdate("Y-m-d\TH:i:s\Z"),
 49:             'Keywords' => 'SimpleExcel.php',
 50:             'LastAuthor' => 'SimpleExcel.php'
 51:         );
 52:     }
 53: 
 54:     /**
 55:      * Get document content as string
 56:      * 
 57:      * @param   array   $options    Options
 58:      * @return  string              Content of document
 59:      */
 60:     public function toString ($options = NULL) {
 61:         $properties = isset($options['properties']) ? $options['properties'] : $this->doc_prop;
 62:         $content = '<?xml version="1.0"?>
 63: <?mso-application progid="Excel.Sheet"?>
 64: <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 65:  xmlns:o="urn:schemas-microsoft-com:office:office"
 66:  xmlns:x="urn:schemas-microsoft-com:office:excel"
 67:  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 68:  xmlns:html="http://www.w3.org/TR/REC-html40">
 69:  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">';
 70:         foreach ($properties as $propname => $propval) {
 71:             $content .= '
 72:   <'.$propname.'>'.$propval.'</'.$propname.'>';
 73:         }
 74:         $content .= '
 75:  </DocumentProperties>';
 76:         foreach ($this->workbook->getWorksheets() as $i => $worksheet) {
 77:             $content .= '
 78:  <Worksheet ss:Name="Sheet' . ($i + 1) . '">
 79:   <Table>';
 80:             foreach ($worksheet->getRecords() as $record) {
 81:                 $content .= '
 82:    <Row>';   
 83:                 foreach ($record as $cell) {
 84:                     $datatype = 'String';
 85:                     switch ($cell->datatype) {
 86:                         case Datatype::NUMBER:
 87:                             $datatype = 'Number';
 88:                             break;
 89:                         case Datatype::LOGICAL:
 90:                             $datatype = 'Boolean';
 91:                             break;
 92:                         case Datatype::DATETIME:
 93:                             $datatype = 'DateTime';
 94:                             break;
 95:                         case Datatype::ERROR:
 96:                             $datatype = 'Error';
 97:                     }
 98:                     $content .= '
 99:     <Cell>
100:      <Data ss:Type="' . $datatype . '">' . $cell->value . '</Data>
101:     </Cell>';
102:                 }
103:                 $content .= '
104:    </Row>';
105:             }
106:             $content .= '
107:   </Table>
108:  </Worksheet>';
109:         }
110:         $content .= '
111: </Workbook>';
112:         return $content;
113:     }
114: }
API documentation generated by ApiGen 2.8.0