25 require_once DOL_DOCUMENT_ROOT.
'/core/modules/export/modules_export.php';
26 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
28 use PhpOffice\PhpSpreadsheet\Spreadsheet;
29 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
30 use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
53 public $version =
'dolibarr';
82 $this->
id =
'excel2007';
83 $this->label =
'Excel 2007';
84 $this->desc = $langs->trans(
'Excel2007FormatDesc');
85 $this->extension =
'xlsx';
86 $this->picto =
'mime/xls';
87 $this->version =
'1.30';
88 $this->phpmin = array(5, 6);
92 if (empty($this->disabled))
94 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
95 $this->label_lib =
'PhpSpreadSheet';
96 $this->version_lib =
'1.6.0';
139 return $this->extension;
149 return $this->version;
159 return $this->label_lib;
169 return $this->version_lib;
184 global $user, $conf, $langs;
186 if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL))
188 $outputlangs->charset_output =
'ISO-8859-1';
191 dol_syslog(get_class($this).
"::open_file file=".$file);
196 $outputlangs->load(
"exports");
198 require_once DOL_DOCUMENT_ROOT.
'/includes/phpoffice/autoloader.php';
199 require_once DOL_DOCUMENT_ROOT.
'/includes/Psr/autoloader.php';
200 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
202 if ($this->
id ==
'excel2007')
204 if (!class_exists(
'ZipArchive'))
206 $langs->load(
"errors");
207 $this->error = $langs->trans(
'ErrorPHPNeedModule',
'zip');
212 $this->workbook =
new Spreadsheet();
213 $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs).
' - '.DOL_APPLICATION_TITLE.
' '.DOL_VERSION);
215 $this->workbook->getProperties()->setTitle(basename($file));
216 $this->workbook->getProperties()->setSubject(basename($file));
217 $this->workbook->getProperties()->setDescription(DOL_APPLICATION_TITLE.
' '.DOL_VERSION);
219 $this->workbook->setActiveSheetIndex(0);
220 $this->workbook->getActiveSheet()->setTitle($outputlangs->trans(
"Sheet"));
221 $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(16);
252 public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
258 $this->workbook->getActiveSheet()->getStyle(
'1')->getFont()->setBold(
true);
259 $this->workbook->getActiveSheet()->getStyle(
'1')->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
262 if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL)) {
265 foreach ($array_selected_sorted as $code => $value)
267 $alias = $array_export_fields_label[$code];
269 if (empty($alias))
dol_print_error(
'',
'Bad value for field with code='.$code.
'. Try to redefine export.');
270 if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL))
272 $this->worksheet->write($this->row, $this->col, $outputlangs->transnoentities($alias), $formatheader);
274 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $outputlangs->transnoentities($alias));
275 if (!empty($array_types[$code]) && in_array($array_types[$code], array(
'Date',
'Numeric',
'TextAuto')))
277 $this->workbook->getActiveSheet()->getColumnDimension($this->
column2Letter($this->col + 1))->setAutoSize(
true);
296 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
303 if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL)) {
309 foreach ($array_selected_sorted as $code => $value)
311 if (strpos($code,
' as ') == 0) $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $code);
312 else $alias = substr($code, strpos($code,
' as ') + 4);
313 if (empty($alias))
dol_print_error(
'',
'Bad value for field with code='.$code.
'. Try to redefine export.');
314 $newvalue = $objp->$alias;
317 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
319 if (preg_match(
'/^Select:/i', $typefield, $reg) && $typefield = substr($typefield, 7))
321 $array = unserialize($typefield);
322 $array = $array[
'options'];
323 $newvalue = $array[$newvalue];
327 if (preg_match(
'/^\((.*)\)$/i', $newvalue, $reg))
329 $newvalue = $outputlangs->transnoentities($reg[1]);
331 $newvalue = $outputlangs->convToOutputCharset($newvalue);
334 if (preg_match(
'/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/i', $newvalue))
337 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
338 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
339 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd');
340 } elseif (preg_match(
'/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/i', $newvalue))
343 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
344 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
345 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd h:mm:ss');
347 if ($typefield ==
'Text' || $typefield ==
'TextAuto')
349 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, (
string) $newvalue);
350 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
351 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'@');
352 $this->workbook->getActiveSheet()->getStyle($coord)->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
354 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
389 $objWriter =
new Xlsx($this->workbook);
390 $objWriter->save($this->file);
391 $this->workbook->disconnectWorksheets();
392 unset($this->workbook);
425 if ($c <= 0)
return '';
429 $c = intval(($c - $p) / 26);
430 $letter = chr(65 + $p).$letter;
447 $this->workbook->getActiveSheet()->setCellValue($startCell, $val);
449 if (!empty($endCell)) {
450 $cellRange = $startCell.
':'.$endCell;
451 $this->workbook->getActiveSheet()->mergeCells($startCell.
':'.$endCell);
453 else $cellRange = $startCell;
454 if (!empty($this->styleArray)) $this->workbook->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->styleArray);
457 $this->error = $e->getMessage();
472 $this->styleArray[
'borders'] = array(
474 'borderStyle' => $thickness,
475 'color' => array(
'argb' => $color)
490 $this->styleArray[
'font'] = array(
491 'color' => array(
'argb' => $color),
505 $this->styleArray[
'alignment'] = array(
'horizontal' => $horizontal);
515 $this->styleArray = array();
527 public function setBlock($startCell, $TDatas = array(), $boldTitle =
false)
530 if (!empty($TDatas)) {
531 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
532 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
533 $startRow = $startCell->getRow();
534 foreach ($TDatas as $column => $TRows) {
535 if ($boldTitle) $this->
setFontStyle(
true, $this->styleArray[
'font'][
'color'][
'argb']);
536 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
539 if ($boldTitle) $this->
setFontStyle(
false, $this->styleArray[
'font'][
'color'][
'argb']);
540 foreach ($TRows as $row) {
542 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $rowPos);
550 $this->error = $e->getMessage();
567 if (!empty($TDatas)) {
568 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
569 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
570 $startRow = $startCell->getRow();
571 foreach ($TDatas as $title => $val) {
572 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
573 if ($boldTitle) $this->
setFontStyle(
true, $this->styleArray[
'font'][
'color'][
'argb']);
575 if ($boldTitle) $this->
setFontStyle(
false, $this->styleArray[
'font'][
'color'][
'argb']);
576 $cell2 = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + 1, $startRow);
583 $this->error = $e->getMessage();
598 foreach (range($firstColumn, $lastColumn) as $columnID) {
599 $this->workbook->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(
true);
616 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
617 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
618 if (!empty($offset)) $startColumn += $offset;
620 $startRow = $startCell->getRow();
621 $startCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
622 $startCoordinate = $startCell->getCoordinate();
625 $endCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + ($length - 1), $startRow);
626 $endCoordinate = $endCell->getCoordinate();
627 $this->workbook->getActiveSheet()->mergeCells($startCoordinate.
':'.$endCoordinate);
630 $this->error = $e->getMessage();
633 return $endCoordinate;
getDriverExtension()
getDriverExtension
Parent class for export modules.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto= 'UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
Output record line into file.
excel_clean($newvalue)
Clean a cell to respect rules of Excel file cells.
getDriverVersion()
getDriverVersion
getDriverDesc()
getDriverDesc
enableAutosize($firstColumn, $lastColumn)
Enable auto sizing for column range.
open_file($file, $outputlangs)
Open output file.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
getLibVersion()
getLibVersion
setBorderStyle($thickness, $color)
Set border style.
setCellValue($val, $startCell, $endCell= '')
Set cell value and automatically merge if we give an endcell.
$conf db
API class for accounts.
setBlock2Columns($startCell, $TDatas=array(), $boldTitle=false)
Make a 2xN Tab in Sheet.
setMergeCellValueByLength($val, $startCell, $length, $offset=0)
Set a value cell and merging it by giving a starting cell and a length.
Class to build export files with Excel format.
write_header($outputlangs)
Write header.
close_file()
Close Excel file.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
setFontStyle($bold, $color)
Set font style.
column2Letter($c)
Convert a column to letter (1->A, 0->B, 27->AA, ...)
setAlignmentStyle($horizontal)
Set alignment style (horizontal, left, right, ...)
write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
Output title line into file.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
write_footer($outputlangs)
Write footer.
setBlock($startCell, $TDatas=array(), $boldTitle=false)
Make a NxN Block in sheet.
__construct($db)
Constructor.
getDriverLabel()
getDriverLabel