-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpreadsheet.php
237 lines (210 loc) · 5.66 KB
/
Spreadsheet.php
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
#
# Spreadsheet.php - a library for generating spreadsheets
# in Excel's XML format
#
# (c) 2014 Kathryn Lybarger. CC-BY-SA
#
class Spreadsheet { # actually a workbook, but who says that?
public $styles; # array of type Style
public $sheets; # array of type Sheet
public $activeSheet; # integer
public $default_style;
public $title_style;
function __construct( ) {
$this->default_style = new Style("Default", "Normal");
$this->title_style = new Style("Title", "Title",
array(
'FontColor' => '#ffffff',
'FontBold' => true,
'BackgroundColor' => '#00ff00',
)
);
$this->link_style = new Style("Hyperlink", "Hyperlink",
array(
'FontColor' => '#0000D4',
'Underline' => 'Single'
)
);
$this->styles = array(
$this->default_style,
$this->title_style,
$this->link_style
);
$this->sheets = array( new Sheet("Sheet1") ) ;
$this->activeSheet = 0;
}
public function asXML() {
$str = '<?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">
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<ActiveSheet>' . $this->activeSheet . '</ActiveSheet>
</ExcelWorkbook>
<Styles>
';
foreach ($this->styles as $style) {
$fontbold = ($style->FontBold)?' ss:Bold="1"':"";
$bgcolor = ($style->BackgroundColor) ?
' ss:Color="' . $style->BackgroundColor . '" ss:Pattern="Solid"'
: "";
$underline = ($style->Underline)?' ss:Underline="' .
$style->Underline . '"':"";
$numberformat = ($style->NumberFormat)?' ss:Format="' .
$style->NumberFormat . '"':"";
$str .= ' <Style ss:ID="' . $style->ID . '" ss:Name="' .
$style->name . '">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="' . $style->FontName .'" x:Family="' .
$style->FontFamily .'" ss:Size="' . $style->FontSize .
'" ss:Color="' . $style->FontColor . '"' . $fontbold . $underline . '/>
<Interior' . $bgcolor . '/>
<NumberFormat' . $numberformat . '/>
<Protection/>
</Style>
';
}
$str .= ' </Styles>
';
foreach ($this->sheets as $sheet) {
$str .= ' <Worksheet ss:Name="' . $sheet->name . '">
<Table>
';
foreach ($sheet->columns as $column) {
if ($column->style != "Default") {
$colstyle = ' ss:StyleID="' . $column->style . '"';
} else {
$colstyle = "";
}
$str .= ' <Column ss:AutoFitWidth="0" ss:Width="' .
$column->width . '"' . $colstyle . '/>
';
}
foreach ($sheet->rows as $row) {
$str .= ' <Row';
if ($row->style != 'Default') {
$str .= ' ss:StyleID="' . $row->style . '"';
}
$str .= '>
';
foreach ($row->cells as $cell) {
$str .= ' <Cell';
if ($cell->style != 'Default') {
$str .= ' ss:StyleID="' . $cell->style . '"';
}
if ($cell->href) {
$str .= ' ss:HRef="' . $cell->href . '"';
}
$str .= '>
';
$str .= ' <Data ss:Type="' . $cell->data->type . '">';
$str .= htmlspecialchars($cell->data->content) . '</Data>
';
$str .= ' </Cell>
';
}
$str .= ' </Row>
';
}
$str .= ' </Table>
';
$str .= ' </Worksheet>
';
}
$str .= '</Workbook>
';
return $str;
}
}
class Style {
public $ID;
public $name;
public $FontName;
public $FontFamily;
public $FontSize;
public $FontColor;
public $FontBold;
public $BackgroundColor;
public $Underline;
public $NumberFormat;
function __construct( $ID, $name, $op = array() ) {
$this->ID = $ID;
$this->name = $name;
$this->FontName = isset($op['FontName'])?$op['FontName']:"Calibri";
$this->FontFamily = isset($op['FontFamily'])?$op['FontFamily']:"Swiss";
$this->FontSize = isset($op['FontSize'])?$op['FontSize']:"11";
$this->FontColor = isset($op['FontColor'])?$op['FontColor']:"#000000";
$this->FontBold = isset($op['FontBold'])?$op['FontBold']:false;
$this->BackgroundColor = isset($op['BackgroundColor'])?$op['BackgroundColor']:null;
$this->Underline = isset($op['Underline'])?$op['Underline']:null;
$this->NumberFormat = isset($op['NumberFormat'])?$op['NumberFormat']:null;
}
}
class Sheet { # a two-by-two array of data
public $name;
public $columns;
public $rows;
function __construct( $name, $op = array() ) {
$this->name = $name;
$this->columns = array();
$this->rows = array();
}
public function appendColumn($column) {
$this->columns[] = $column;
}
public function appendRow($row) {
$this->rows[] = $row;
}
function addTitleRow( $data, $op = array() ) {
$row = new Row();
$row->populate( $data );
$row->style = "Title";
$this->rows = array($row);
}
public function deleteAllRows() {
$this->rows = array();
}
}
class Column { # contains no data, mainly style
public $width;
public $style;
function __construct( $op = array() ) {
$this->width = (isset($op['width']))?$op['width']:null;
$this->style = (isset($op['style']))?$op['style']:"Default";
}
}
class Row {
public $cells;
public $style;
function __construct( $op = array() ) {
$this->style = "Default";
}
function populate( $data ) {
$this->cells = array();
foreach ($data as $datum) {
$this->cells[] = new Cell($datum);
}
}
}
class Cell {
public $href;
public $style;
public $data;
function __construct( $data, $op = array() ) {
$this->style = "Default";
$this->data = new Data($data);
}
}
class Data {
public $type;
public $content;
function __construct( $data, $op = array() ) {
$this->content = $data;
$this->type = "String";
}
}