1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package fwolflib |
4
|
|
|
* @subpackage class |
5
|
|
|
* @copyright Copyright 2003-2009, Fwolf |
6
|
|
|
* @author Fwolf <[email protected]> |
7
|
|
|
* @since 2009-12-22 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
require_once(dirname(__FILE__) . '/fwolflib.php'); |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @package fwolflib |
16
|
|
|
* @subpackage class |
17
|
|
|
* @copyright Copyright 2003-2009, Fwolf |
18
|
|
|
* @author Fwolf <[email protected]> |
19
|
|
|
* @since 2009-12-22 |
20
|
|
|
* @link http://www.phplamp.org/2008/06/php-to-excel-clas/ |
21
|
|
|
*/ |
22
|
|
|
class Excel extends Fwolflib { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Array of row in xml |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $aRow = array(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Footer of excel xml |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $sFooter = "</Workbook>"; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Header of excel xml |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $sHeader = '<?xml version="1.0" encoding="utf-8"?> |
41
|
|
|
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" |
42
|
|
|
xmlns:x="urn:schemas-microsoft-com:office:excel" |
43
|
|
|
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" |
44
|
|
|
xmlns:html="http://www.w3.org/TR/REC-html40"> |
45
|
|
|
'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Title of sheet |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
public $sSheetTitle = 'Sheet1'; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set a single row(add mode) |
56
|
|
|
* |
57
|
|
|
* @param array $ar 1-dimensional array |
58
|
|
|
*/ |
59
|
|
|
protected function SetRow($ar) { |
60
|
|
|
$s_cell = ''; |
61
|
|
|
|
62
|
|
|
if (!empty($ar)) { |
63
|
|
|
foreach ($ar as $v) { |
64
|
|
|
// Attention: data type |
65
|
|
|
if(is_numeric($v)) { |
66
|
|
|
$v = strval($v); |
67
|
|
|
// First letter is '0' ? |
68
|
|
|
if(0 == $v{0}) { |
69
|
|
|
$s_cells.= '<Cell><Data ss:Type="string">' . $v . '</Data></Cell> |
|
|
|
|
70
|
|
|
'; |
71
|
|
|
} else { |
72
|
|
|
$s_cell .= '<Cell><Data ss:Type="number">' . $v . '</Data></Cell> |
73
|
|
|
'; |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
$s_cell .= '<Cell><Data ss:Type="string">' . $v . '</Data></Cell> |
77
|
|
|
'; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
$this->aRow[] = "<Row>\n$s_cell</Row>\n"; |
81
|
|
|
} |
82
|
|
|
} // end of func SetRow |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set data rows, multi row, clean mode |
87
|
|
|
* |
88
|
|
|
* @param array $ar 2-dimensional array |
89
|
|
|
*/ |
90
|
|
|
public function SetRows ($ar) { |
91
|
|
|
if (!empty($ar)) |
92
|
|
|
foreach ($ar as $v) |
93
|
|
|
$this->SetRow($v); |
94
|
|
|
} // end of func SetRows |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the worksheet title |
99
|
|
|
* |
100
|
|
|
* Checks the string for not allowed characters (:\/?*), |
101
|
|
|
* cuts it to maximum 31 characters and set the title. Damn |
102
|
|
|
* why are not-allowed chars nowhere to be found? Windows |
103
|
|
|
* help's no help... |
104
|
|
|
* |
105
|
|
|
* @param string $title |
106
|
|
|
*/ |
107
|
|
|
public function SetSheetTitle($title) { |
108
|
|
|
// Strip special chars |
109
|
|
|
$title = str_replace ( |
110
|
|
|
array(':', '\\', '/', '?', '*'), |
111
|
|
|
'', $title); |
112
|
|
|
|
113
|
|
|
// Cut it to the allowed length |
114
|
|
|
$title = substr($title, 0, 31); |
115
|
|
|
|
116
|
|
|
$this->sSheetTitle = $title; |
117
|
|
|
} // end of func SetSheetTitle |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Output the excel file |
122
|
|
|
* |
123
|
|
|
* @param string $fn Filename without '.xls' |
124
|
|
|
*/ |
125
|
|
|
public function Output($fn) { |
126
|
|
|
// Set header |
127
|
|
|
header('Content-Type: application/vnd.ms-excel; charset=utf-8'); |
128
|
|
|
header('Content-Disposition: inline; filename="' |
129
|
|
|
. $fn . '.xls"'); |
130
|
|
|
|
131
|
|
|
// Print |
132
|
|
|
echo ($this->sHeader); |
133
|
|
|
echo '<Worksheet ss:Name="' |
134
|
|
|
. $this->sSheetTitle |
135
|
|
|
. '"> |
136
|
|
|
<Table> |
137
|
|
|
'; |
138
|
|
|
echo '<Column ss:Index="1" ss:AutoFitWidth="0" ss:Width="110"/> |
139
|
|
|
'; |
140
|
|
|
echo implode("\n", $this->aRow); |
141
|
|
|
echo '</Table> |
142
|
|
|
</Worksheet> |
143
|
|
|
'; |
144
|
|
|
echo $this->sFooter; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} // end of class Excel |
148
|
|
|
|
149
|
|
|
/* |
150
|
|
|
// Usage |
151
|
|
|
// Need high version of microsoft office |
152
|
|
|
|
153
|
|
|
$ar = array ( |
154
|
|
|
array ('列1', '列2', '列3列3列3', '123456'), |
155
|
|
|
); |
156
|
|
|
$xls = new Excel; |
157
|
|
|
$xls->SetRows($ar); |
158
|
|
|
$xls->Output('test'); |
159
|
|
|
*/ |
160
|
|
|
|
161
|
|
|
?> |
162
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: