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);
99 return [self::FORMAT_XML, self::FORMAT_BIFF];
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($a_value)) {
194 if (is_string($a_value)) {
195 if (!mb_check_encoding($a_value,
'UTF-8')) {
211 case $a_value instanceof
ilDate:
227 return $a_value ? $lng->
txt(
'yes') : $lng->
txt(
'no');
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(
264 $cell = $wb->getCell($a_coords);
266 } elseif (is_numeric($a_value)) {
267 $this->workbook->getActiveSheet()->setCellValueExplicit(
270 DataType::TYPE_NUMERIC
273 $this->workbook->getActiveSheet()->setCellValueExplicit(
276 DataType::TYPE_STRING
292 ?
string $a_datatype = null
296 if ($a_datatype !== null) {
297 $this->workbook->getActiveSheet()->setCellValueExplicit(
303 $wb = $this->workbook->getActiveSheet()->setCellValue(
308 } elseif (is_numeric($a_value)) {
309 $this->workbook->getActiveSheet()->setCellValueExplicit(
312 DataType::TYPE_NUMERIC
315 $this->workbook->getActiveSheet()->setCellValueExplicit(
318 DataType::TYPE_STRING
332 string $a_top_left =
'A1',
335 foreach ($a_values as $row_idx => $cols) {
336 if (is_array($cols)) {
337 foreach ($cols as $col_idx => $col_value) {
338 $a_values[$row_idx][$col_idx] = $this->
prepareValue($col_value);
345 $this->workbook->getActiveSheet()->fromArray($a_values, $a_null_value, $a_top_left);
358 return $this->workbook->getActiveSheet()->getCell($coordinate)->getValue();
366 return $this->workbook->getActiveSheet()->toArray();
374 return Coordinate::columnIndexFromString($this->workbook->getActiveSheet()->getHighestDataColumn());
383 return Coordinate::stringFromColumnIndex($col);
392 foreach ($this->workbook->getWorksheetIterator() as $worksheet) {
393 $this->workbook->setActiveSheetIndex($this->workbook->getIndex($worksheet));
394 $sheet = $this->workbook->getActiveSheet();
395 $cellIterator = $sheet->getRowIterator()->current()->getCellIterator();
396 $cellIterator->setIterateOnlyExistingCells(
true);
397 foreach ($cellIterator as $cell) {
398 $sheet->getColumnDimension($cell->getColumn())->setAutoSize(
true);
413 $this->workbook->setActiveSheetIndex(0);
415 switch ($this->format) {
416 case self::FORMAT_BIFF:
417 if (stripos($a_file_name,
'.xls') ===
false) {
418 $a_file_name .=
'.xls';
422 case self::FORMAT_XML:
423 if (stripos($a_file_name,
'.xlsx') ===
false) {
424 $a_file_name .=
'.xlsx';
439 switch ($this->format) {
440 case self::FORMAT_BIFF:
441 $a_mime_type = MimeType::APPLICATION__VND_MS_EXCEL;
444 case self::FORMAT_XML:
445 $a_mime_type = MimeType::APPLICATION__VND_OPENXMLFORMATS_OFFICEDOCUMENT_SPREADSHEETML_SHEET;
448 $a_mime_type = MimeType::APPLICATION__OCTET_STREAM;
453 $writer = IOFactory::createWriter($this->workbook, $this->format);
454 $writer->save($tmp_name);
468 $writer = IOFactory::createWriter($this->workbook, $this->format);
469 $writer->save($a_file);
477 $writer = IOFactory::createWriter($this->workbook, $this->format);
487 public function setBold(
string $a_coords): void
489 $this->workbook->getActiveSheet()->getStyle($a_coords)->getFont()->setBold(
true);
497 string $a_background,
498 string $a_font = null
502 'fillType' => Fill::FILL_SOLID,
503 'color' => [
'rgb' => $a_background]
509 'color' => [
'rgb' => $a_font]
513 $this->workbook->getActiveSheet()->getStyle($a_coords)->applyFromArray($opts);
522 bool $a_right =
false,
523 bool $a_bottom =
false,
526 $style = $this->workbook->getActiveSheet()->getStyle($a_coords);
530 $style->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN);
533 $style->getBorders()->getRight()->setBorderStyle(Border::BORDER_THIN);
536 $style->getBorders()->getBottom()->setBorderStyle(Border::BORDER_THIN);
539 $style->getBorders()->getLeft()->setBorderStyle(Border::BORDER_THIN);
551 $columnLetter = Coordinate::stringFromColumnIndex($col);
553 return $columnLetter . $pRow;
565 $this->workbook->getActiveSheet()->getCell($coordinate)->getHyperlink()->setUrl($a_path);
583 $this->workbook->getActiveSheet()->mergeCells($coordinatesRange);
588 return mb_ereg_replace(
'[' . implode(
'', $this->noncharacters) .
']',
'', $string);
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
prepareBooleanValue(bool $a_value)
prepareStorage(string $a_file_name)
Prepare workbook for storage/delivery.
getSheetAsArray()
Returns the active sheet as an array.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
setDateFormat(Cell $a_cell, $a_value)
Set date format of cell.
getColumnCoord(int $a_col)
Get column "name" from number.
setBorders(string $a_coords, bool $a_top, bool $a_right=false, bool $a_bottom=false, bool $a_left=false)
Toggle cell(s) borders.
getColumnCount()
Returns the number of columns the sheet contains.
addLink(int $a_row, int $a_column, string $a_path)
setCell(int $a_row, int $a_col, $a_value, ?string $a_datatype=null)
Set cell value.
cleanupNonCharachters(string $string)
setFormat(string $a_format)
Set file format.
setGlobalAutoSize()
Set all existing columns on all sheets to autosize.
setBold(string $a_coords)
Set cell(s) to bold.
static deliverFileAttached(string $path_to_file, ?string $download_file_name=null, ?string $mime_type=null, bool $delete_file=false)
getSheetTitle()
Return the current sheet title.
getCoordByColumnAndRow(int $pColumn=1, int $pRow=1)
Get cell coordinate (e.g.
getSheetCount()
Returns number of sheets.
writeToFile(string $a_file)
Save workbook to file.
addSheet(string $a_name, bool $a_activate=true)
Add sheet.
getCell(int $a_row, int $a_col)
Returns the value of a cell.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setCellByCoordinates($a_coords, $a_value)
Set cell value by coordinates.
columnIndexAdjustment(int $column)
Adjustment needed because of migration PHPExcel to PhpSpreadsheet.
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
setCellArray(array $a_values, string $a_top_left='A1', $a_null_value=null)
Set cell values from array.
mergeCells(string $coordinatesRange)
prepareDateValue(ilDateTime $a_value)
setActiveSheet(int $a_index)
static shortenTextExtended(string $a_str, int $a_len, bool $a_dots=false, bool $a_next_blank=false, bool $a_keep_extension=false)
loadFromFile(string $filename)
Loads a spreadsheet from file.
prepareString(string $a_value)
setColors(string $a_coords, string $a_background, string $a_font=null)
Set cell(s) colors.
sendToClient(string $a_file_name)
Send workbook to client.
prepareValue($a_value)
Prepare value for cell.