-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgridCSVGenerator.php
117 lines (94 loc) · 2.28 KB
/
gridCSVGenerator.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
<?php
class gridHTMLGenerator {
public $strip_tags = false;
protected $headerCols = Array();
protected $footerCols = Array();
public function printGrid($xml) {
$this->renderStart();
$this->headerParse($xml->head);
$this->renderHeader($this->headerCols);
$this->renderRows($xml->row);
$this->footerParse($xml->foot);
$this->renderFooter($this->footerCols);
$this->renderEnd();
}
private function headerParse($header) {
if (isset($header->column)) {
$columns = Array($header->column);
} else {
$columns = $header->columns;
}
foreach ($columns as $row) {
$cols = Array();
foreach ($row as $column) {
$hidden = ($column->attributes()->hidden == 'true') ? true : false;
if ($hidden == true) {
$this->hiddenCols[$k] = true;
continue;
}
$col = $this->strip(trim((string) $column));
$cols[] = $col;
}
$this->headerCols[] = $cols;
}
}
private function renderStart() {
header("Content-type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: attachment;filename=grid.xls");
header("Cache-Control: max-age=0");
}
private function renderEnd() { }
private function renderHeader($cols) {
for ($i = 0; $i < count($cols); $i++) {
for ($j = 0; $j < count($cols[$i]); $j++) {
echo $cols[$i][$j]."\t";
}
echo "\n";
}
}
private function footerParse($footer) {
if (isset($footer->columns)) {
$columns = $footer->columns;
foreach ($columns as $row) {
$cols = Array();
foreach ($row as $column) {
$col = $this->strip(trim((string) $column));
$cols[] = $col;
}
$this->footerCols[] = $cols;
}
}
}
private function renderFooter($cols) {
for ($i = 0; $i < count($cols); $i++) {
for ($j = 0; $j < count($cols[$i]); $j++)
echo $cols[$i][$j]."\t";
echo "\n";
}
}
private function renderRows($rows) {
$i = 0;
foreach ($rows as $row) {
$className = ($i%2 == 0) ? "cell_even" : "cell_odd";
$j = 0;
foreach ($row as $cell) {
if (isset($this->hiddenCols[$j])) {
$j++;
continue;
}
$text = $this->strip((string) $cell);
echo $text."\t";
$j++;
}
echo "\n";
$i++;
}
}
private function strip($param) {
if ($this->strip_tags == true) {
$param = strip_tags($param);
}
return trim($param);
}
}
?>