1: <?php
2:
3: namespace SimpleExcel\Writer;
4:
5: use SimpleExcel\Enums\Datatype;
6:
7: 8: 9: 10: 11: 12:
13: class XMLWriter extends BaseWriter
14: {
15: 16: 17: 18: 19: 20:
21: protected $content_type = 'application/xml';
22:
23: 24: 25: 26: 27: 28:
29: protected $file_extension = 'xml';
30:
31: 32: 33: 34: 35: 36:
37: private $doc_prop;
38:
39: 40: 41: 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: 56: 57: 58: 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: }