forked from ILIAS-eLearning/ILIAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.ilExcel.php
More file actions
533 lines (462 loc) · 10.5 KB
/
class.ilExcel.php
File metadata and controls
533 lines (462 loc) · 10.5 KB
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
<?php
/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
include_once './libs/composer/vendor/autoload.php';
/*
* Wrapper for Microsoft Excel Import/Export (based on PHPExcel)
*
* @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
* @ingroup ServicesExcel
*/
class ilExcel
{
/**
* @var PHPExcel
*/
protected $workbook; // [PHPExcel]
/**
* @var string
*/
protected $type; // [string]
const FORMAT_XML = "Excel2007";
const FORMAT_BIFF = "Excel5";
/**
* Constructor
*
* @return self
*/
public function __construct()
{
$this->setFormat(self::FORMAT_XML);
$this->workbook = new PHPExcel();
$this->workbook->removeSheetByIndex(0);
}
//
// loading files
//
/**
* Loads a spreadsheet from file
* @param $filename
*/
public function loadFromFile($filename) {
$this->workbook = PHPExcel_IOFactory::load($filename);
}
//
// type/format
//
/**
* Get valid file formats
*
* @return array
*/
public function getValidFormats()
{
return array(self::FORMAT_XML, self::FORMAT_BIFF);
}
/**
* Set file format
*
* @param string $a_format
*/
public function setFormat($a_format)
{
if(in_array($a_format, $this->getValidFormats()))
{
$this->format = $a_format;
}
}
//
// sheets
//
/**
* Add sheet
*
* @param string $a_name
* @param bool $a_activate
* @return int index
*/
public function addSheet($a_name, $a_activate = true)
{
// see PHPExcel_Worksheet::$_invalidCharacters;
$invalid = array('*', ':', '/', '\\', '?', '[', ']');
$a_name = str_replace($invalid, "", $a_name);
// #19056 - phpExcel only allows 31 chars
// see https://github.com/PHPOffice/PHPExcel/issues/79
$a_name = ilUtil::shortenText($a_name, 31);
$sheet = new PHPExcel_Worksheet($this->workbook, $a_name);
$this->workbook->addSheet($sheet);
$new_index = $this->workbook->getSheetCount()-1;
if((bool)$a_activate)
{
$this->setActiveSheet($new_index);
}
return $new_index;
}
/**
* Set active sheet
*
* @param int $a_index
*/
public function setActiveSheet($a_index)
{
$this->workbook->setActiveSheetIndex($a_index);
}
/**
* Returns number of sheets
*
* @return int
*/
public function getSheetCount() {
return $this->workbook->getSheetCount();
}
/**
* Return the current sheet title
*
* @return string
*/
public function getSheetTitle() {
return $this->workbook->getActiveSheet()->getTitle();
}
//
// cells
//
/**
* Prepare value for cell
*
* @param mixed $a_value
* @return mixed
*/
protected function prepareValue($a_value)
{
global $lng;
// :TODO: does this make sense?
if(is_bool($a_value))
{
$a_value = $this->prepareBooleanValue($a_value);
}
else if($a_value instanceof ilDateTime)
{
$a_value = $this->prepareDateValue($a_value);
}
else if(is_string($a_value))
{
$a_value = $this->prepareString($a_value);
}
return $a_value;
}
/**
* @param ilDateTime $a_value
* @return string
*/
protected function prepareDateValue(ilDateTime $a_value)
{
switch(true)
{
case $a_value instanceof ilDate:
$a_value = PHPExcel_Shared_Date::stringToExcel($a_value->get(IL_CAL_DATE));
break;
default:
$a_value = PHPExcel_Shared_Date::stringToExcel($a_value->get(IL_CAL_DATETIME));
break;
}
return $a_value;
}
/**
* @param bool $a_value
* @return string
*/
protected function prepareBooleanValue($a_value)
{
global $lng;
return $a_value ? $lng->txt('yes') : $lng->txt('no');
}
/**
* @param string $a_value
* @return string
*/
protected function prepareString($a_value)
{
return strip_tags($a_value); // #14542
}
/**
* Set date format
*
* @param PHPExcel_Cell $a_cell
* @param mixed $a_value
*/
protected function setDateFormat(PHPExcel_Cell $a_cell, $a_value)
{
if($a_value instanceof ilDate)
{
// :TODO: i18n?
$a_cell->getStyle()->getNumberFormat()->setFormatCode("dd.mm.yyyy");
}
else if($a_value instanceof ilDateTime)
{
// :TODO: i18n?
$a_cell->getStyle()->getNumberFormat()->setFormatCode("dd.mm.yyyy hh:mm:ss");
}
}
/**
* Set cell value by coordinates
*
* @param string $a_coords
* @param mixed $a_value
*/
public function setCellByCoordinates($a_coords, $a_value)
{
$cell = $this->workbook->getActiveSheet()->setCellValue(
$a_coords,
$this->prepareValue($a_value),
true
);
$this->setDateFormat($cell, $a_value);
}
/**
* Set cell value
*
* @param int $a_row
* @param int $a_col
* @param mixed $a_value
*/
public function setCell($a_row, $a_col, $a_value)
{
$cell = $this->workbook->getActiveSheet()->setCellValueByColumnAndRow(
$a_col,
$a_row,
$this->prepareValue($a_value),
true
);
$this->setDateFormat($cell, $a_value);
}
/**
* Set cell values from array
*
* @param array $a_values
* @param string $a_top_left
* @param mixed $a_null_value
*/
public function setCellArray(array $a_values, $a_top_left = "A1", $a_null_value = NULL)
{
foreach($a_values as $row_idx => $cols)
{
if(is_array($cols))
{
foreach($cols as $col_idx => $col_value)
{
$a_values[$row_idx][$col_idx] = $this->prepareValue($col_value);
}
}
else
{
$a_values[$row_idx] = $this->prepareValue($cols);
}
}
$this->workbook->getActiveSheet()->fromArray($a_values, $a_null_value, $a_top_left);
}
/**
* Returns the value of a cell
*
* @param int $a_row
* @param int $a_col
*
* @return mixed
*/
public function getCell($a_row, $a_col) {
return $this->workbook->getActiveSheet()->getCellByColumnAndRow($a_col, $a_row)->getValue();
}
/**
* Returns the active sheet as an array
*
* @return array
*/
public function getSheetAsArray() {
return $this->workbook->getActiveSheet()->toArray();
}
/**
* Returns the number of columns the sheet contains
*
* @return int
*/
public function getColumnCount() {
return PHPExcel_Cell::columnIndexFromString($this->workbook->getActiveSheet()->getHighestDataColumn());
}
/**
* Get column "name" from number
*
* @param int $a_col
* @return string
*/
public function getColumnCoord($a_col)
{
return PHPExcel_Cell::stringFromColumnIndex($a_col);
}
/**
* Set all existing columns on all sheets to autosize
*/
protected function setGlobalAutoSize()
{
// this may change the active sheet
foreach($this->workbook->getWorksheetIterator() as $worksheet)
{
$this->workbook->setActiveSheetIndex($this->workbook->getIndex($worksheet));
$sheet = $this->workbook->getActiveSheet();
$cellIterator = $sheet->getRowIterator()->current()->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach($cellIterator as $cell)
{
$sheet->getColumnDimension($cell->getColumn())->setAutoSize(true);
}
}
}
//
// deliver/save
//
/**
* Prepare workbook for storage/delivery
*/
protected function prepareStorage($a_file_name)
{
$this->setGlobalAutoSize();
$this->workbook->setActiveSheetIndex(0);
switch($this->format)
{
case self::FORMAT_BIFF:
if(!stristr($a_file_name, ".xls"))
{
$a_file_name .= ".xls";
}
break;
case self::FORMAT_XML:
if(!stristr($a_file_name, ".xlsx"))
{
$a_file_name .= ".xlsx";
}
break;
}
return $a_file_name;
}
/**
* Send workbook to client
*
* @param string $a_file_name
*/
public function sendToClient($a_file_name)
{
require_once('./Services/FileDelivery/classes/class.ilPHPOutputDelivery.php');
$a_file_name = $this->prepareStorage($a_file_name);
switch ($this->format) {
case self::FORMAT_BIFF:
$a_mime_type = ilMimeTypeUtil::APPLICATION__VND_MS_EXCEL;
break;
case self::FORMAT_XML:
$a_mime_type = ilMimeTypeUtil::APPLICATION__VND_OPENXMLFORMATS_OFFICEDOCUMENT_SPREADSHEETML_SHEET;
break;
default:
$a_mime_type = ilMimeTypeUtil::APPLICATION__OCTET_STREAM;
break;
}
$ilPHPOutputDelivery = new ilPHPOutputDelivery();
$ilPHPOutputDelivery->start($a_file_name, $a_mime_type);
$writer = PHPExcel_IOFactory::createWriter($this->workbook, $this->format);
$writer->save(ilFileDelivery::DIRECT_PHP_OUTPUT);
$ilPHPOutputDelivery->stop();
}
/**
* Save workbook to file
*
* @param string $a_file full path
*/
public function writeToFile($a_file)
{
$a_file = $this->prepareStorage($a_file);
$writer = PHPExcel_IOFactory::createWriter($this->workbook, $this->format);
$writer->save($a_file);
}
/**
* @return string
* @throws \PHPExcel_Reader_Exception
*/
public function writeToTmpFile() {
$writer = PHPExcel_IOFactory::createWriter($this->workbook, $this->format);
$filename = ilUtil::ilTempnam();
$writer->save($filename);
return $filename;
}
//
// style (:TODO: more options?)
//
/**
* Set cell(s) to bold
*
* @param string $a_coords
*/
public function setBold($a_coords)
{
$this->workbook->getActiveSheet()->getStyle($a_coords)->getFont()->setBold(true);
}
/**
* Set cell(s) colors
*
* @param string $a_coords
* @param string $a_background
* @param string $a_font
*/
public function setColors($a_coords, $a_background, $a_font = null)
{
$opts = array(
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => $a_background)
)
);
if($a_font)
{
$opts['font'] = array(
'color' => array('rgb' => $a_font)
);
}
$this->workbook->getActiveSheet()->getStyle($a_coords)->applyFromArray($opts);
}
/**
* Toggle cell(s) borders
*
* @param string $a_coords
* @param bool $a_top
* @param bool $a_right
* @param bool $a_bottom
* @param bool $a_left
*/
public function setBorders($a_coords, $a_top, $a_right = false, $a_bottom = false, $a_left = false)
{
$style = $this->workbook->getActiveSheet()->getStyle($a_coords);
// :TODO: border styles?
if($a_top)
{
$style->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
}
if($a_right)
{
$style->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
}
if($a_bottom)
{
$style->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
}
if($a_left)
{
$style->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
}
}
/**
* Get cell coordinate (e.g. "B2") for column and row number
*
* @param int $pColumn
* @param int $pRow
* @return string
*/
function getCoordByColumnAndRow($pColumn = 0, $pRow = 1)
{
$columnLetter = PHPExcel_Cell::stringFromColumnIndex($pColumn);
return $columnLetter . $pRow;
}
}