19use PhpOffice\PhpSpreadsheet\Spreadsheet;
20use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
21use PhpOffice\PhpSpreadsheet\IOFactory;
22use PhpOffice\PhpSpreadsheet\Shared\Date;
23use PhpOffice\PhpSpreadsheet\Cell\Cell;
24use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
25use PhpOffice\PhpSpreadsheet\Style\Fill;
26use PhpOffice\PhpSpreadsheet\Style\Border;
27use PhpOffice\PhpSpreadsheet\Cell\DataType;
42 '\x{1FFFE}-\x{1FFFF}',
43 '\x{2FFFE}-\x{2FFFF}',
44 '\x{3FFFE}-\x{3FFFF}',
45 '\x{4FFFE}-\x{4FFFF}',
46 '\x{5FFFE}-\x{5FFFF}',
47 '\x{6FFFE}-\x{6FFFF}',
48 '\x{7FFFE}-\x{7FFFF}',
49 '\x{8FFFE}-\x{8FFFF}',
50 '\x{9FFFE}-\x{9FFFF}',
51 '\x{AFFFE}-\x{AFFFF}',
52 '\x{BFFFE}-\x{BFFFF}',
53 '\x{CFFFE}-\x{CFFFF}',
54 '\x{DFFFE}-\x{DFFFF}',
55 '\x{EFFFE}-\x{EFFFF}',
56 '\x{FFFFE}-\x{FFFFF}',
57 '\x{10FFFE}-\x{10FFFF}',
72 $this->
lng = $DIC->language();
74 $this->workbook =
new Spreadsheet();
75 $this->workbook->removeSheetByIndex(0);
87 $this->workbook = IOFactory::load(
$filename);
108 $this->format = $a_format;
124 bool $a_activate =
true
128 $invalid = [
'*',
':',
'/',
'\\',
'?',
'[',
']',
'\'-',
'\''];
130 $a_name = str_replace($invalid,
'', $a_name);
136 $sheet =
new Worksheet($this->workbook, $a_name);
137 $this->workbook->addSheet($sheet);
138 $new_index = $this->workbook->getSheetCount() - 1;
152 $this->workbook->setActiveSheetIndex($a_index);
161 return $this->workbook->getSheetCount();
170 return $this->workbook->getActiveSheet()->getTitle();
186 if (is_bool($value)) {
187 return $this->prepareBooleanValue($value);
191 return $this->prepareDateValue($value);
194 if (is_string($value)) {
195 return $this->prepareString($value);
208 case $a_value instanceof
ilDate:
224 return $a_value ?
$lng->txt(
'yes') :
$lng->txt(
'no');
230 if (!mb_check_encoding($value,
'UTF-8')) {
231 throw new InvalidArgumentException(
'Invalid UTF-8 passed.');
234 return $this->cleanupNonCharachters(strip_tags($value));
245 if ($a_value instanceof
ilDate) {
246 $a_cell->getStyle()->getNumberFormat()->setFormatCode(
'dd.mm.yyyy');
248 $a_cell->getStyle()->getNumberFormat()->setFormatCode(
'dd.mm.yyyy hh:mm:ss');
260 $wb = $this->workbook->getActiveSheet()->setCellValue(
262 $this->prepareValue($a_value)
264 $cell = $wb->getCell($a_coords);
265 $this->setDateFormat($cell, $a_value);
266 } elseif (is_numeric($a_value)) {
267 $this->workbook->getActiveSheet()->setCellValueExplicit(
269 $this->prepareValue($a_value),
270 DataType::TYPE_NUMERIC
273 $this->workbook->getActiveSheet()->setCellValueExplicit(
275 $this->prepareValue($a_value),
276 DataType::TYPE_STRING
292 ?
string $datatype =
null
294 $coordinate = $this->getCoordByColumnAndRow($col, $a_row);
296 if ($datatype === DataType::TYPE_STRING) {
297 $this->workbook->getActiveSheet()->setCellValueExplicit(
299 $this->prepareString($value),
302 } elseif ($datatype !==
null) {
303 $this->workbook->getActiveSheet()->setCellValueExplicit(
305 $this->prepareValue($value),
309 $wb = $this->workbook->getActiveSheet()->setCellValue(
311 $this->prepareValue($value)
313 $this->setDateFormat($wb->getCell($coordinate), $value);
314 } elseif (is_numeric($value)) {
315 $this->workbook->getActiveSheet()->setCellValueExplicit(
317 $this->prepareValue($value),
318 DataType::TYPE_NUMERIC
321 $this->workbook->getActiveSheet()->setCellValueExplicit(
323 $this->prepareValue($value),
324 DataType::TYPE_STRING
338 string $a_top_left =
'A1',
341 foreach ($a_values as $row_idx => $cols) {
342 if (is_array($cols)) {
343 foreach ($cols as $col_idx => $col_value) {
344 $a_values[$row_idx][$col_idx] = $this->prepareValue($col_value);
347 $a_values[$row_idx] = $this->prepareValue($cols);
351 $this->workbook->getActiveSheet()->fromArray($a_values, $a_null_value, $a_top_left);
363 $coordinate = $this->getCoordByColumnAndRow($a_col, $a_row);
364 return $this->workbook->getActiveSheet()->getCell($coordinate)->getValue();
372 return $this->workbook->getActiveSheet()->toArray();
380 return Coordinate::columnIndexFromString($this->workbook->getActiveSheet()->getHighestDataColumn());
388 $col = $this->columnIndexAdjustment($a_col);
389 return Coordinate::stringFromColumnIndex($col);
398 foreach ($this->workbook->getWorksheetIterator() as $worksheet) {
399 $this->workbook->setActiveSheetIndex($this->workbook->getIndex($worksheet));
400 $sheet = $this->workbook->getActiveSheet();
401 $cellIterator = $sheet->getRowIterator()->current()->getCellIterator();
402 $cellIterator->setIterateOnlyExistingCells(
true);
403 foreach ($cellIterator as $cell) {
404 $sheet->getColumnDimension($cell->getColumn())->setAutoSize(
true);
418 $this->setGlobalAutoSize();
419 $this->workbook->setActiveSheetIndex(0);
421 switch ($this->format) {
422 case self::FORMAT_BIFF:
423 if (stripos($a_file_name,
'.xls') ===
false) {
424 $a_file_name .=
'.xls';
428 case self::FORMAT_XML:
429 if (stripos($a_file_name,
'.xlsx') ===
false) {
430 $a_file_name .=
'.xlsx';
444 $a_file_name = $this->prepareStorage($a_file_name);
445 switch ($this->format) {
446 case self::FORMAT_BIFF:
447 $a_mime_type = MimeType::APPLICATION__VND_MS_EXCEL;
450 case self::FORMAT_XML:
451 $a_mime_type = MimeType::APPLICATION__VND_OPENXMLFORMATS_OFFICEDOCUMENT_SPREADSHEETML_SHEET;
454 $a_mime_type = MimeType::APPLICATION__OCTET_STREAM;
459 $writer = IOFactory::createWriter($this->workbook, $this->format);
460 $writer->save($tmp_name);
472 $a_file = $this->prepareStorage($a_file);
474 $writer = IOFactory::createWriter($this->workbook, $this->format);
475 $writer->save($a_file);
483 $writer = IOFactory::createWriter($this->workbook, $this->format);
493 public function setBold(
string $a_coords): void
495 $this->workbook->getActiveSheet()->getStyle($a_coords)->getFont()->setBold(
true);
503 string $a_background,
504 ?
string $a_font =
null
508 'fillType' => Fill::FILL_SOLID,
509 'color' => [
'rgb' => $a_background]
515 'color' => [
'rgb' => $a_font]
519 $this->workbook->getActiveSheet()->getStyle($a_coords)->applyFromArray($opts);
528 bool $a_right =
false,
529 bool $a_bottom =
false,
532 $style = $this->workbook->getActiveSheet()->getStyle($a_coords);
536 $style->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN);
539 $style->getBorders()->getRight()->setBorderStyle(Border::BORDER_THIN);
542 $style->getBorders()->getBottom()->setBorderStyle(Border::BORDER_THIN);
545 $style->getBorders()->getLeft()->setBorderStyle(Border::BORDER_THIN);
556 $col = $this->columnIndexAdjustment($pColumn);
557 $columnLetter = Coordinate::stringFromColumnIndex($col);
559 return $columnLetter . $pRow;
570 $coordinate = $this->getCoordByColumnAndRow($a_column, $a_row);
571 $this->workbook->getActiveSheet()->getCell($coordinate)->getHyperlink()->setUrl($a_path);
589 $this->workbook->getActiveSheet()->mergeCells($coordinatesRange);
594 return mb_ereg_replace(
'[' . implode(
'', $this->noncharacters) .
']',
'', $string);
@classDescription Date and time handling
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
prepareValue($value)
Prepare value for cell.
setBold(string $a_coords)
Set cell(s) to bold.
getCell(int $a_row, int $a_col)
Returns the value of a cell.
prepareStorage(string $a_file_name)
Prepare workbook for storage/delivery.
getSheetAsArray()
Returns the active sheet as an array.
getSheetCount()
Returns number of sheets.
getColumnCoord(int $a_col)
Get column "name" from number.
addSheet(string $a_name, bool $a_activate=true)
Add sheet.
loadFromFile(string $filename)
Loads a spreadsheet from file.
setGlobalAutoSize()
Set all existing columns on all sheets to autosize.
setBorders(string $a_coords, bool $a_top, bool $a_right=false, bool $a_bottom=false, bool $a_left=false)
Toggle cell(s) borders.
getCoordByColumnAndRow(int $pColumn=1, int $pRow=1)
Get cell coordinate (e.g.
writeToFile(string $a_file)
Save workbook to file.
columnIndexAdjustment(int $column)
Adjustment needed because of migration PHPExcel to PhpSpreadsheet.
sendToClient(string $a_file_name)
Send workbook to client.
setDateFormat(Cell $a_cell, $a_value)
Set date format of cell.
setCellArray(array $a_values, string $a_top_left='A1', $a_null_value=null)
Set cell values from array.
setCellByCoordinates($a_coords, $a_value)
Set cell value by coordinates.
setColors(string $a_coords, string $a_background, ?string $a_font=null)
Set cell(s) colors.
setActiveSheet(int $a_index)
mergeCells(string $coordinatesRange)
getColumnCount()
Returns the number of columns the sheet contains.
prepareString(string $value,)
prepareDateValue(ilDateTime $a_value)
getSheetTitle()
Return the current sheet title.
setFormat(string $a_format)
Set file format.
setCell(int $a_row, int $col, $value, ?string $datatype=null)
Set cell value.
prepareBooleanValue(bool $a_value)
addLink(int $a_row, int $a_column, string $a_path)
cleanupNonCharachters(string $string)
static deliverFileAttached(string $path_to_file, ?string $download_file_name=null, ?string $mime_type=null, bool $delete_file=false)
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static shortenTextExtended(string $a_str, int $a_len, bool $a_dots=false, bool $a_next_blank=false, bool $a_keep_extension=false)
if(!file_exists('../ilias.ini.php'))