ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
XMLWriter.php
Go to the documentation of this file.
1<?php
2
3namespace SimpleExcel\Writer;
4
11class XMLWriter extends BaseWriter implements IWriter
12{
19 protected $content_type = 'application/xml';
20
27 protected $file_extension = 'xml';
28
35 private $doc_prop;
36
40 public function __construct(){
41 $this->doc_prop = array(
42 'Author' => 'SimpleExcel',
43 'Company' => 'SimpleExcel',
44 'Created' => gmdate("Y-m-d\TH:i:s\Z"),
45 'Keywords' => 'SimpleExcel',
46 'LastAuthor' => 'SimpleExcel',
47 'Version' => '12.00'
48 );
49 }
50
57 public function addRow($values){
59 $row .= '
60 <Row ss:AutoFitHeight="0">';
61
62 foreach($values as $val){
63
64 $value = '';
65 $datatype = 'String';
66
67 // check if given variable contains array
68 if(is_array($val)){
69 $value = $val[0];
70 $datatype = $val[1];
71 } else {
72 $value = $val;
73 $datatype = is_string($val) ? 'String' : (is_numeric($val) ? 'Number' : 'String');
74 }
75
76 // escape value from HTML tags
77 $value = filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS);
78
79 $row .= '
80 <Cell><Data ss:Type="'.$datatype.'">'.$value.'</Data></Cell>';
81 }
82
83 $row .= '
84 </Row>';
85 }
86
92 public function saveString(){
93 $content = '<?xml version="1.0"?>
94<?mso-application progid="Excel.Sheet"?>
95<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
96 xmlns:o="urn:schemas-microsoft-com:office:office"
97 xmlns:x="urn:schemas-microsoft-com:office:excel"
98 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
99 xmlns:html="http://www.w3.org/TR/REC-html40">
100 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">';
101
102 foreach($this->doc_prop as $propname => $propval){
103 $content .= '
104 <'.$propname.'>'.$propval.'</'.$propname.'>';
105 }
106
107 $content .= '
108 </DocumentProperties>
109 <Worksheet ss:Name="Sheet1">
110 <Table>'.$this->tabl_data.'
111 </Table>
112 </Worksheet>
113</Workbook>';
114 return $content;
115 }
116
123 public function setData($values){
124 if(!is_array($values)){
125 $values = array($values);
126 }
127 $this->tabl_data = ""; // reset the xml data.
128
129 // append values as rows
130 foreach ($values as $value) {
131 $this->addRow($value);
132 }
133 }
134
142 public function setDocProp($prop, $val){
143 $this->doc_prop[$prop] = $val;
144 }
145}
146?>
saveString()
Get document content as string.
Definition: XMLWriter.php:92
setDocProp($prop, $val)
Set a document property of the XML.
Definition: XMLWriter.php:142
addRow($values)
Adding row data to XML.
Definition: XMLWriter.php:57
setData($values)
Set XML data.
Definition: XMLWriter.php:123
define writer interface
Definition: IWriter.php:14