-
Notifications
You must be signed in to change notification settings - Fork 7
/
tlbExcelView.php
executable file
·556 lines (504 loc) · 23.9 KB
/
tlbExcelView.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php
Yii::import('zii.widgets.grid.CGridView');
/**
* @author Nikola Kostadinov
* @license MIT License
* @version 0.3
* @link http://yiiframework.com/extension/eexcelview/
*
* @fork 0.33ab
* @forkversion 1.1
* @author A. Bennouna
* @organization tellibus.com
* @license MIT License
* @link https://github.com/tellibus/tlbExcelView
*/
/* Usage :
$this->widget('application.components.widgets.tlbExcelView', array(
'id' => 'some-grid',
'dataProvider' => $model->search(),
'grid_mode' => $production, // Same usage as EExcelView v0.33
//'template' => "{summary}\n{items}\n{exportbuttons}\n{pager}",
'title' => 'Some title - ' . date('d-m-Y - H-i-s'),
'creator' => 'Your Name',
'subject' => mb_convert_encoding('Something important with a date in French: ' . utf8_encode(strftime('%e %B %Y')), 'ISO-8859-1', 'UTF-8'),
'description' => mb_convert_encoding('Etat de production généré à la demande par l\'administrateur (some text in French).', 'ISO-8859-1', 'UTF-8'),
'lastModifiedBy' => 'Some Name',
'sheetTitle' => 'Report on ' . date('m-d-Y H-i'),
'keywords' => '',
'category' => '',
'landscapeDisplay' => true, // Default: false
'A4' => true, // Default: false - ie : Letter (PHPExcel default)
'RTL' => false, // Default: false
'pageFooterText' => '&RThis is page no. &P of &N pages', // Default: '&RPage &P of &N'
'automaticSum' => true, // Default: false
'decimalSeparator' => ',', // Default: '.'
'thousandsSeparator' => '.', // Default: ','
//'displayZeros' => false,
//'zeroPlaceholder' => '-',
'sumLabel' => 'Column totals:', // Default: 'Totals'
'borderColor' => '00FF00', // Default: '000000'
'bgColor' => 'FFFF00', // Default: 'FFFFFF'
'textColor' => 'FF0000', // Default: '000000'
'rowHeight' => 45, // Default: 15
'headerBorderColor' => 'FF0000', // Default: '000000'
'headerBgColor' => 'CCCCCC', // Default: 'CCCCCC'
'headerTextColor' => '0000FF', // Default: '000000'
'headerHeight' => 10, // Default: 20
'footerBorderColor' => '0000FF', // Default: '000000'
'footerBgColor' => '00FFCC', // Default: 'FFFFCC'
'footerTextColor' => 'FF00FF', // Default: '0000FF'
'footerHeight' => 50, // Default: 20
'columns' => $grid // an array of your CGridColumns
)); */
class tlbExcelView extends CGridView
{
//the PHPExcel object
public $libPath = 'ext.phpexcel.Classes.PHPExcel'; //the path to the PHP excel lib
public static $objPHPExcel = null;
public static $activeSheet = null;
//Document properties
public $creator = 'Nikola Kostadinov';
public $title = null;
public $subject = 'Subject';
public $description = '';
public $category = '';
public $lastModifiedBy = 'A. Bennouna';
public $keywords = '';
public $sheetTitle = '';
public $legal = 'PHPExcel generator http://phpexcel.codeplex.com/ - EExcelView Yii extension http://yiiframework.com/extension/eexcelview/ - Adaptation by A. Bennouna http://tellibus.com';
public $landscapeDisplay = false;
public $A4 = false;
public $RTL = false;
public $pageFooterText = '&RPage &P of &N';
//config
public $autoWidth = true;
public $exportType = 'Excel5';
public $disablePaging = true;
public $filename = null; //export FileName
public $stream = true; //stream to browser
public $grid_mode = 'grid'; //Whether to display grid ot export it to selected format. Possible values(grid, export)
public $grid_mode_var = 'grid_mode'; //GET var for the grid mode
//options
public $automaticSum = false;
public $sumLabel = 'Totals';
public $decimalSeparator = '.';
public $thousandsSeparator = ',';
public $displayZeros = false;
public $zeroPlaceholder = '-';
public $border_style;
public $borderColor = '000000';
public $bgColor = 'FFFFFF';
public $textColor = '000000';
public $rowHeight = 15;
public $headerBorderColor = '000000';
public $headerBgColor = 'CCCCCC';
public $headerTextColor = '000000';
public $headerHeight = 20;
public $footerBorderColor = '000000';
public $footerBgColor = 'FFFFCC';
public $footerTextColor = '0000FF';
public $footerHeight = 20;
public $zoomScale = 100;
public static $fill_solid;
public static $papersize_A4;
public static $orientation_landscape;
public static $horizontal_center;
public static $horizontal_right;
public static $vertical_center;
public static $style = array();
public static $headerStyle = array();
public static $footerStyle = array();
public static $summableColumns = array();
//buttons config
public $exportButtonsCSS = 'summary';
public $exportButtons = array('Excel2007');
public $exportText = 'Export to: ';
//callbacks
public $onRenderHeaderCell = null;
public $onRenderDataCell = null;
public $onRenderFooterCell = null;
public $onSetSheetProperties = null;
//mime types used for streaming
public $mimeTypes = array(
'Excel5' => array(
'Content-type'=>'application/vnd.ms-excel',
'extension'=>'xls',
'caption'=>'Excel(*.xls)',
),
'Excel2007' => array(
'Content-type'=>'application/vnd.ms-excel',
'extension'=>'xlsx',
'caption'=>'Excel(*.xlsx)',
),
'PDF' =>array(
'Content-type'=>'application/pdf',
'extension'=>'pdf',
'caption'=>'PDF(*.pdf)',
),
'HTML' =>array(
'Content-type'=>'text/html',
'extension'=>'html',
'caption'=>'HTML(*.html)',
),
'CSV' =>array(
'Content-type'=>'application/csv',
'extension'=>'csv',
'caption'=>'CSV(*.csv)',
)
);
public function init()
{
if (isset($_GET[$this->grid_mode_var])) {
$this->grid_mode = $_GET[$this->grid_mode_var];
}
if (isset($_GET['exportType'])) {
$this->exportType = $_GET['exportType'];
}
$lib = Yii::getPathOfAlias($this->libPath).'.php';
if (($this->grid_mode == 'export') && (!file_exists($lib))) {
$this->grid_mode = 'grid';
Yii::log("PHP Excel lib not found($lib). Export disabled !", CLogger::LEVEL_WARNING, 'EExcelview');
}
if ($this->grid_mode == 'export') {
if (!isset($this->title)) {
$this->title = Yii::app()->getController()->getPageTitle();
}
$this->initColumns();
//parent::init();
//Autoload fix
spl_autoload_unregister(array('YiiBase','autoload'));
Yii::import($this->libPath, true);
// Get here some PHPExcel constants in order to use them elsewhere
self::$papersize_A4 = PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4;
self::$orientation_landscape = PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE;
self::$fill_solid = PHPExcel_Style_Fill::FILL_SOLID;
if (!isset($this->border_style)) {
$this->border_style = PHPExcel_Style_Border::BORDER_THIN;
}
self::$horizontal_center = PHPExcel_Style_Alignment::HORIZONTAL_CENTER;
self::$horizontal_right = PHPExcel_Style_Alignment::HORIZONTAL_RIGHT;
self::$vertical_center = PHPExcel_Style_Alignment::VERTICAL_CENTER;
spl_autoload_register(array('YiiBase','autoload'));
// Creating a workbook
self::$objPHPExcel = new PHPExcel();
self::$activeSheet = self::$objPHPExcel->getActiveSheet();
// Set some basic document properties
if ($this->landscapeDisplay) {
self::$activeSheet->getPageSetup()->setOrientation(self::$orientation_landscape);
}
if ($this->A4) {
self::$activeSheet->getPageSetup()->setPaperSize(self::$papersize_A4);
}
if ($this->RTL) {
self::$activeSheet->setRightToLeft(true);
}
self::$objPHPExcel->getProperties()
->setTitle($this->title)
->setCreator($this->creator)
->setSubject($this->subject)
->setDescription($this->description . ' // ' . $this->legal)
->setCategory($this->category)
->setLastModifiedBy($this->lastModifiedBy)
->setKeywords($this->keywords);
// Initialize styles that will be used later
self::$style = array(
'borders' => array(
'allborders' => array(
'style' => $this->border_style,
'color' => array('rgb' => $this->borderColor),
),
),
'fill' => array(
'type' => self::$fill_solid,
'color' => array('rgb' => $this->bgColor),
),
'font' => array(
//'bold' => false,
'color' => array('rgb' => $this->textColor),
)
);
self::$headerStyle = array(
'borders' => array(
'allborders' => array(
'style' => $this->border_style,
'color' => array('rgb' => $this->headerBorderColor),
),
),
'fill' => array(
'type' => self::$fill_solid,
'color' => array('rgb' => $this->headerBgColor),
),
'font' => array(
'bold' => true,
'color' => array('rgb' => $this->headerTextColor),
)
);
self::$footerStyle = array(
'borders' => array(
'allborders' => array(
'style' => $this->border_style,
'color' => array('rgb' => $this->footerBorderColor),
),
),
'fill' => array(
'type' => self::$fill_solid,
'color' => array('rgb' => $this->footerBgColor),
),
'font' => array(
'bold' => true,
'color' => array('rgb' => $this->footerTextColor),
)
);
} else {
parent::init();
}
}
public function renderHeader()
{
$a = 0;
foreach ($this->columns as $column) {
$a = $a + 1;
if ($column instanceof CButtonColumn) {
$head = $column->header;
} else if (($column->header === null) && ($column->name !== null)) {
if($column->grid->dataProvider instanceof CActiveDataProvider) {
$head = $column->grid->dataProvider->model->getAttributeLabel($column->name);
} else {
$head = $column->name;
}
} else {
$head =trim($column->header)!=='' ? $column->header : $column->grid->blankDisplay;
}
$cell = self::$activeSheet->setCellValue($this->columnName($a) . '1', $head, true);
if (is_callable($this->onRenderHeaderCell)) {
call_user_func_array($this->onRenderHeaderCell, array($cell, $head));
}
}
// Format the header row
$header = self::$activeSheet->getStyle($this->columnName(1) . '1:' . $this->columnName($a) . '1');
$header->getAlignment()
->setHorizontal(self::$horizontal_center)
->setVertical(self::$vertical_center);
$header->applyFromArray(self::$headerStyle);
self::$activeSheet->getRowDimension(1)->setRowHeight($this->headerHeight);
}
public function renderBody()
{
if ($this->disablePaging) {
//if needed disable paging to export all data
$this->dataProvider->pagination = false;
}
$data = $this->dataProvider->getData(true);
$n = count($data);
if ($n > 0) {
for ($row = 0; $row < $n; ++$row) {
$this->renderRow($row);
}
}
return $n;
}
public function renderRow($row)
{
$data = $this->dataProvider->getData();
$a = 0;
foreach ($this->columns as $n => $column) {
if ($column instanceof CLinkColumn) {
if ($column->labelExpression !== null) {
$value = $column->evaluateExpression($column->labelExpression, array('data' => $data[$row], 'row' => $row));
} else {
$value = $column->label;
}
} else if ($column instanceof CButtonColumn) {
$value = ""; //Dont know what to do with buttons
} else if ($column->value !== null) {
$value = $this->evaluateExpression($column->value, array('data' => $data[$row]));
} else if ($column->name !== null) {
//$value = $data[$row][$column->name];
$value = CHtml::value($data[$row], $column->name);
$value = $value === null ? "" : $column->grid->getFormatter()->format($value, 'raw');
}
$a++;
// Check if the cell value is a number, then format it accordingly
// May be improved notably by exposing the formats as public
// May be usable only for French-style number formatting ?
if (preg_match("/^[0-9]*\\" . $this->thousandsSeparator . "[0-9]*\\" . $this->decimalSeparator . "[0-9]*$/", strip_tags($value))) {
$content = str_replace($this->decimalSeparator, '.', str_replace($this->thousandsSeparator, '', strip_tags($value)));
$format = '#\.##0.00';
} else if (preg_match("/^[0-9]*\\" . $this->decimalSeparator . "[0-9]*$/", strip_tags($value))) {
$content = str_replace($this->decimalSeparator, '.', strip_tags($value));
$format = '0.00';
} else if (!$this->displayZeros && ((strip_tags($value) === '0') || (strip_tags($value) === $this->zeroPlaceholder))) {
$content = $this->zeroPlaceholder;
self::$activeSheet->getStyle($this->columnName($a) . ($row + 2))->getAlignment()->setHorizontal(self::$horizontal_right);
$format = '0.00';
} else {
$content = strip_tags($value);
$format = null;
}
$cell = self::$activeSheet->setCellValue($this->columnName($a) . ($row + 2), $content, true);
// Format each cell's number - if any
if (!is_null($format)) {
self::$summableColumns[$a] = $a;
self::$activeSheet->getStyle($this->columnName($a) . ($row + 2))->getNumberFormat()->setFormatCode($format);
}
// Cell Value callback.
if(is_callable($this->onRenderDataCell)) {
// Summable Column has to be passed as pointer and separate from its array (and later overwritten), because pass-by-reference doesn't work as expected.
$summableColumn = empty(self::$summableColumns[$a]) ? null : self::$summableColumns[$a];
call_user_func_array($this->onRenderDataCell, array(&$cell, $data[$row], $value, $a, $column, &$summableColumn));
// Overwrite summable Column
if (empty($summableColumn))
unset(self::$summableColumns[$a]);
else
self::$summableColumns[$a] = $summableColumn;
}
}
// Format the row globally
$renderedRow = self::$activeSheet->getStyle('A' . ($row + 2) . ':' . $this->columnName($a) . ($row + 2));
$renderedRow->getAlignment()->setVertical(self::$vertical_center);
$renderedRow->applyFromArray(self::$style);
self::$activeSheet->getRowDimension($row + 2)->setRowHeight($this->rowHeight);
}
public function renderFooter($row)
{
$a = 0;
foreach ($this->columns as $n => $column) {
$a = $a + 1;
if ($column->footer) {
$footer = trim($column->footer) !== '' ? $column->footer : $column->grid->blankDisplay;
$cell = self::$activeSheet->setCellValue($this->columnName($a) . ($row + 2), $footer, true);
if(is_callable($this->onRenderFooterCell)) {
call_user_func_array($this->onRenderFooterCell, array($cell, $footer));
}
} else if ($this->automaticSum && in_array($a, self::$summableColumns)) {
// We want to render automatic sums in the footer if no footer was already present in the grid
$cell = self::$activeSheet->setCellValue($this->columnName($a) . ($row + 2), '=SUM(' . $this->columnName($a) . '2:' . $this->columnName($a) . ($row + 1) . ')', true);
$sum = self::$activeSheet->getCell($this->columnName($a) . ($row + 2))->getCalculatedValue();
if ($sum < 1000) {
$format = '0' . $this->decimalSeparator . '00';
} else if ($sum < 1000000) {
$format = '#\\' . $this->thousandsSeparator . '##0' . $this->decimalSeparator . '00';
} else {
$format = '#\\' . $this->thousandsSeparator . '###\\' . $this->thousandsSeparator . '##0' . $this->decimalSeparator . '00';
}
// We won't set the whole row's borders and number format, so proceed with each cell individually
self::$activeSheet->getStyle($this->columnName($a) . ($row + 2))
->applyFromArray(self::$footerStyle)
->getNumberFormat()->setFormatCode($format);
if(is_callable($this->onRenderFooterCell)) {
call_user_func_array($this->onRenderFooterCell, array($cell, $footer));
}
// Add a label before the first summable column (supposing it's not the first…)
if (current(self::$summableColumns) == $a) {
$cell = self::$activeSheet->setCellValue($this->columnName($a - 1) . ($row + 2), $this->sumLabel, true);
self::$activeSheet->getStyle($this->columnName($a - 1) . ($row + 2))
->applyFromArray(array('font' => array('bold' => true)))
->getAlignment()->setHorizontal(self::$horizontal_right);
if(is_callable($this->onRenderFooterCell)) {
call_user_func_array($this->onRenderFooterCell, array($cell, $footer));
}
}
}
}
// Some global formatting for the footer in case of automatic sum
if ($this->automaticSum) {
self::$activeSheet->getStyle('A' . ($row + 2) . ':' . $this->columnName($a) . ($row + 2))->getAlignment()->setVertical(self::$vertical_center);
self::$activeSheet->getRowDimension($row + 2)->setRowHeight($this->footerHeight);
}
}
public function run()
{
if ($this->grid_mode == 'export') {
$this->renderHeader();
$row = $this->renderBody();
$this->renderFooter($row);
//set auto width
if ($this->autoWidth) {
foreach ($this->columns as $n => $column) {
$cell = self::$activeSheet->getColumnDimension($this->columnName($n + 1))->setAutoSize(true);
}
}
// Set some additional properties
self::$activeSheet
->setTitle($this->sheetTitle)
->getSheetView()->setZoomScale($this->zoomScale);
self::$activeSheet->getHeaderFooter()
->setOddHeader('&C' . $this->sheetTitle)
->setOddFooter('&L&B' . self::$objPHPExcel->getProperties()->getTitle() . $this->pageFooterText);
self::$activeSheet->getPageSetup()
->setPrintArea('A1:' . $this->columnName(count($this->columns)) . ($row + 2))
->setFitToWidth();
// Sheet Properties Callback.
if(is_callable($this->onSetSheetProperties)) {
call_user_func_array($this->onSetSheetProperties, array($this, self::$activeSheet));
}
//create writer for saving
$objWriter = PHPExcel_IOFactory::createWriter(self::$objPHPExcel, $this->exportType);
if (!$this->stream) {
$objWriter->save($this->filename);
} else {
//output to browser
if(!$this->filename) {
$this->filename = $this->title;
}
$this->cleanOutput();
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-type: '.$this->mimeTypes[$this->exportType]['Content-type']);
header('Content-Disposition: attachment; filename="' . $this->filename . '.' . $this->mimeTypes[$this->exportType]['extension'] . '"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
Yii::app()->end();
}
} else {
parent::run();
}
}
/**
* Returns the corresponding Excel column.(Abdul Rehman from yii forum)
*
* @param int $index
* @return string
*/
public function columnName($index)
{
--$index;
if (($index >= 0) && ($index < 26)) {
return chr(ord('A') + $index);
} else if ($index > 25) {
return ($this->columnName($index / 26)) . ($this->columnName($index%26 + 1));
} else {
throw new Exception("Invalid Column # " . ($index + 1));
}
}
public function renderExportButtons()
{
foreach ($this->exportButtons as $key => $button) {
$item = is_array($button) ? CMap::mergeArray($this->mimeTypes[$key], $button) : $this->mimeTypes[$button];
$type = is_array($button) ? $key : $button;
$url = parse_url(Yii::app()->request->requestUri);
//$content[] = CHtml::link($item['caption'], '?'.$url['query'].'exportType='.$type.'&'.$this->grid_mode_var.'=export');
if (key_exists('query', $url)) {
$content[] = CHtml::link($item['caption'], '?' . $url['query'] . '&exportType=' . $type . '&' . $this->grid_mode_var . '=export');
} else {
$content[] = CHtml::link($item['caption'], '?exportType=' . $type . '&' . $this->grid_mode_var . '=export');
}
}
if ($content) {
echo CHtml::tag('div', array('class' => $this->exportButtonsCSS), $this->exportText.implode(', ', $content));
}
}
/**
* Performs cleaning on mutliple levels.
*
* From le_top @ yiiframework.com
*
*/
private static function cleanOutput()
{
for ($level = ob_get_level(); $level > 0; --$level) {
@ob_end_clean();
}
}
}