-
Notifications
You must be signed in to change notification settings - Fork 3
/
classExcel.php
231 lines (197 loc) · 8.41 KB
/
classExcel.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
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of classExcel
*
* @author tr
*/
namespace trExcel
{
require_once 'PHPExcel/Classes/PHPExcel.php';
class Excel
{
public function index($app)
{
$data = $this->getData();
return $app['twig']->render('index.html.twig', array('data' => $data));
}
private function getData()
{
$dbh = new \PDO('mysql:host=localhost;dbname=sitepoint', 'root', 'tr0210');
$sql = 'select * from lakers';
$q = $dbh->query($sql);
$res = $q->fetchAll(\PDO::FETCH_ASSOC);
return $res;
}
public function export($app)
{
$data = $this->getData();
//Create an Excel instance
$ea = new \PHPExcel();
//Add some meta information
$this->setMeta($ea);
$ews1 = $this->setData($ea, $data);
$ews2 = $this->addAnalysis($ea);
//The below line should be moved into addAnalysis but we move this here to show that $ews2 is actually referring to the sheet
$ews2->getStyle('b4')->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00);
$this->addChart1($ea, $ews2);
$this->addChart2($ea, $ews1);
//Save it
$writer = \PHPExcel_IOFactory::createWriter($ea, 'Excel2007');
$writer->setIncludeCharts(true);
$writer->save('output.xlsx');
return $app['twig']->render('export.html.twig');
}
private function setMeta($ea)
{
$ea->getProperties()
->setCreator('Taylor Ren')
->setTitle('PHPExcel Demo')
->setLastModifiedBy('Taylor Ren')
->setDescription('A demo to show how to use PHPExcel to manipulate an Excel file')
->setSubject('PHP Excel manipulation')
->setKeywords('excel php office phpexcel lakers')
->setCategory('programming')
;
}
private function setData(\PHPExcel $ea, $data)
{
//Set up the header
$ews = $ea->getSheet(0);
$ews->setTitle('Data');
$ews->setCellValue('a1', 'ID');
$ews->setCellValue('b1', 'Season');
$ews->setCellValue('c1', 'Teams');
$ews->setCellValue('d1', 'Self Score');
$ews->setCellValue('e1', 'Opponent Score');
$ews->setCellValue('f1', 'Date Played');
$ews->setCellValue('g1', 'Win/Lose');
$ews->setCellValue('h1', 'Remark');
//Fill data
$ews->fromArray($data, ' ', 'A2');
//Apply header some styles
$header = 'a1:h1';
$ews->getStyle($header)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('00ffff00');
$style = array(
'font' => array('bold' => true,),
'alignment' => array('horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,),
);
$ews->getStyle($header)->applyFromArray($style);
for ($col = ord('a'); $col <= ord('h'); $col++)
{
$ews->getColumnDimension(chr($col))->setAutoSize(true);
}
return $ews;
}
private function addAnalysis(\PHPExcel $ea)
{
$ews2 = new \PHPExcel_Worksheet($ea, 'Summary');
$ea->addSheet($ews2, 0);
$ews2->setTitle('Summary');
$ews2->setCellValue('a1', 'Lakers 2013-2014 Season');
$style = array(
'font' => array('bold' => true, 'size' => 20,),
'alignment' => array('horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_LEFT,),
);
$ews2->mergeCells('a1:b1');
$ews2->getStyle('a1')->applyFromArray($style);
$ews2->getColumnDimension('a')->setAutoSize(true);
$ews2->setCellValue('a2', 'Win:');
$ews2->setCellValue('a3', 'Loss:');
$ews2->setCellValue('a4', '%:');
$ews2->setCellValue('b2', '=COUNTIF(Data!G2:G91, "W")-COUNTIF(Data!G2:G9, "W")');
$ews2->setCellValue('b3', '=COUNTIF(Data!G2:G91, "L")-COUNTIF(Data!G2:G9, "L")');
$ews2->setCellValue('b4', '=b2/(b2+b3)');
//$ews2->getStyle('b4')->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00);
return $ews2;
}
private function addChart1(\PHPExcel $ea, \PHPExcel_Worksheet $ews)
{
//The below line should be moved into addAnalysis but we move this here to show that $ews is actually referring to the sheet
$ews->getStyle('b4')->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE);
$title = new \PHPExcel_Chart_Title($ews->getTitle());
// Set the data serie labels
$dsl = array(new \PHPExcel_Chart_DataSeriesValues('String', 'Summary!A1', NULL, 1),
);
// Set X-Axis Labels
$xal = array(new \PHPExcel_Chart_DataSeriesValues('String', 'Summary!A2:A3', NULL, 2),);
// Set data serie values
$dsv = array(new \PHPExcel_Chart_DataSeriesValues('Number', 'Summary!B2:B3', NULL, 2),);
// Build a dataserie
$ds = new \PHPExcel_Chart_DataSeries(
\PHPExcel_Chart_DataSeries::TYPE_PIECHART,
null,
range(0, count($dsv)-1),
$dsl,
$xal,
$dsv
);
// A layout for the Pie Chart
$layout=new \PHPExcel_Chart_Layout();
$layout->setShowVal(true);
$layout->setShowPercent(true);
// Set series in the plot area
$pa=new \PHPExcel_Chart_PlotArea($layout, array($ds));
// Set legend
$legend=new \PHPExcel_Chart_Legend(\PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
//Create Chart
$chart= new \PHPExcel_Chart(
'chart1',
$title,
$legend,
$pa,
true,
0,
NULL,
NULL
);
$chart->setTopLeftPosition('K1');
$chart->setBottomRightPosition('U30');
$ews->addChart($chart);
}
private function addChart2(\PHPExcel $ea, \PHPExcel_Worksheet $ews)
{
$title=new \PHPExcel_Chart_Title($ews->getTitle());
$dsl=array(
new \PHPExcel_Chart_DataSeriesValues('String', 'Data!$D$1', NULL, 1),
new \PHPExcel_Chart_DataSeriesValues('String', 'Data!$E$1', NULL, 1),
);
$xal=array(
new \PHPExcel_Chart_DataSeriesValues('String', 'Data!$F$2:$F$91', NULL, 90),
);
$dsv=array(
new \PHPExcel_Chart_DataSeriesValues('Number', 'Data!$D$2:$D$91', NULL, 90),
new \PHPExcel_Chart_DataSeriesValues('Number', 'Data!$E$2:$E$91', NULL, 90),
);
$ds=new \PHPExcel_Chart_DataSeries(
\PHPExcel_Chart_DataSeries::TYPE_LINECHART,
\PHPExcel_Chart_DataSeries::GROUPING_STANDARD,
range(0, count($dsv)-1),
$dsl,
$xal,
$dsv
);
$pa=new \PHPExcel_Chart_PlotArea(NULL, array($ds));
// Set legend
$legend=new \PHPExcel_Chart_Legend(\PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$chart=new \PHPExcel_Chart(
'Chart1',
$title,
$legend,
$pa,
true,
0,
NULL,
NULL
);
$chart->setTopLeftPosition('I1');
$chart->setBottomRightPosition('AA21');
$ews->addChart($chart);
return $chart;
}
}
}