-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathXMLWriter.php
More file actions
146 lines (128 loc) · 3.6 KB
/
XMLWriter.php
File metadata and controls
146 lines (128 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace SimpleExcel\Writer;
/**
* SimpleExcel class for writing Microsoft Excel 2003 XML Spreadsheet
*
* @author Faisalman
* @package SimpleExcel
*/
class XMLWriter extends BaseWriter implements IWriter
{
/**
* Defines content-type for HTTP header
*
* @access protected
* @var string
*/
protected $content_type = 'application/xml';
/**
* Defines file extension to be used when saving file
*
* @access protected
* @var string
*/
protected $file_extension = 'xml';
/**
* Array containing document properties
*
* @access private
* @var array
*/
private $doc_prop;
/**
* @return void
*/
public function __construct(){
$this->doc_prop = array(
'Author' => 'SimpleExcel',
'Company' => 'SimpleExcel',
'Created' => gmdate("Y-m-d\TH:i:s\Z"),
'Keywords' => 'SimpleExcel',
'LastAuthor' => 'SimpleExcel',
'Version' => '12.00'
);
}
/**
* Adding row data to XML
*
* @param array $values An array contains ordered value for every cell
* @return void
*/
public function addRow($values, $end = true){
$row = &$this->tabl_data;
$row .= '
<Row ss:AutoFitHeight="0">';
foreach($values as $val){
$value = '';
$datatype = 'String';
// check if given variable contains array
if(is_array($val)){
$value = $val[0];
$datatype = $val[1];
} else {
$value = $val;
$datatype = is_numeric($val) ? 'Number' : 'String';
}
// escape value from HTML tags
$value = filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS);
$row .= '
<Cell><Data ss:Type="'.$datatype.'">'.$value.'</Data></Cell>';
}
$row .= '
</Row>';
}
/**
* Get document content as string
*
* @return string Content of document
*/
public function saveString(){
$content = '<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">';
foreach($this->doc_prop as $propname => $propval){
$content .= '
<'.$propname.'>'.$propval.'</'.$propname.'>';
}
$content .= '
</DocumentProperties>
<Worksheet ss:Name="Sheet1">
<Table>'.$this->tabl_data.'
</Table>
</Worksheet>
</Workbook>';
return $content;
}
/**
* Set XML data
*
* @param array $values An array contains ordered value of arrays for all fields
* @return void
*/
public function setData($values){
if(!is_array($values)){
$values = array($values);
}
$this->tabl_data = ""; // reset the xml data.
// append values as rows
foreach ($values as $value) {
$this->addRow($value);
}
}
/**
* Set a document property of the XML
*
* @param string $prop Document property to be set
* @param string $val Value of the document property
* @return void
*/
public function setDocProp($prop, $val){
$this->doc_prop[$prop] = $val;
}
}
?>