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(7, 0);
92 if (empty($this->disabled)) {
93 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
94 $this->label_lib =
'PhpSpreadSheet';
95 $this->version_lib =
'1.6.0';
138 return $this->extension;
148 return $this->version;
158 return $this->label_lib;
168 return $this->version_lib;
183 global $user, $conf, $langs;
185 if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL)) {
186 $outputlangs->charset_output =
'ISO-8859-1';
189 dol_syslog(get_class($this).
"::open_file file=".$file);
194 $outputlangs->load(
"exports");
196 require_once DOL_DOCUMENT_ROOT.
'/includes/phpoffice/phpspreadsheet/src/autoloader.php';
197 require_once DOL_DOCUMENT_ROOT.
'/includes/Psr/autoloader.php';
198 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
200 if ($this->
id ==
'excel2007') {
201 if (!class_exists(
'ZipArchive')) {
202 $langs->load(
"errors");
203 $this->error = $langs->trans(
'ErrorPHPNeedModule',
'zip');
208 $this->workbook =
new Spreadsheet();
209 $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs).
' - '.DOL_APPLICATION_TITLE.
' '.DOL_VERSION);
211 $this->workbook->getProperties()->setTitle(basename($file));
212 $this->workbook->getProperties()->setSubject(basename($file));
213 $this->workbook->getProperties()->setDescription(DOL_APPLICATION_TITLE.
' '.DOL_VERSION);
215 $this->workbook->setActiveSheetIndex(0);
216 $this->workbook->getActiveSheet()->setTitle($outputlangs->trans(
"Sheet"));
217 $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(16);
248 public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
254 $this->workbook->getActiveSheet()->getStyle(
'1')->getFont()->setBold(
true);
255 $this->workbook->getActiveSheet()->getStyle(
'1')->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
256 $selectlabel = array();
259 if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL)) {
262 foreach ($array_selected_sorted as $code => $value) {
263 $alias = $array_export_fields_label[$code];
266 dol_print_error(
'',
'Bad value for field with code='.$code.
'. Try to redefine export.');
268 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
270 if (preg_match(
'/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
271 $selectlabel[$code.
"_label"] = $alias.
"_label";
273 if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL)) {
274 $this->worksheet->write($this->row, $this->col, $outputlangs->transnoentities($alias), $formatheader);
276 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $outputlangs->transnoentities($alias));
277 if (!empty($array_types[$code]) && in_array($array_types[$code], array(
'Date',
'Numeric',
'TextAuto'))) {
278 $this->workbook->getActiveSheet()->getColumnDimension($this->
column2Letter($this->col + 1))->setAutoSize(
true);
283 foreach ($selectlabel as $key => $value) {
284 if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL)) {
285 $this->worksheet->write($this->row, $this->col, $outputlangs->transnoentities($value), $formatheader);
287 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $outputlangs->transnoentities($value));
288 if (!empty($array_types[$code]) && in_array($array_types[$code], array(
'Date',
'Numeric',
'TextAuto'))) {
289 $this->workbook->getActiveSheet()->getColumnDimension($this->
column2Letter($this->col + 1))->setAutoSize(
true);
308 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
315 if (!empty($conf->global->MAIN_USE_PHP_WRITEEXCEL)) {
320 $selectlabelvalues = array();
321 foreach ($array_selected_sorted as $code => $value) {
322 if (strpos($code,
' as ') == 0) {
323 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $code);
325 $alias = substr($code, strpos($code,
' as ') + 4);
328 dol_print_error(
'',
'Bad value for field with code='.$code.
'. Try to redefine export.');
330 $newvalue = $objp->$alias;
333 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
335 if (preg_match(
'/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
337 if (is_array($array) && !empty($newvalue)) {
338 $array = $array[
'options'];
339 $selectlabelvalues[$code.
"_label"] = $array[$newvalue];
341 $selectlabelvalues[$code.
"_label"] =
"";
346 if (preg_match(
'/^\((.*)\)$/i', $newvalue, $reg)) {
347 $newvalue = $outputlangs->transnoentities($reg[1]);
349 $newvalue = $outputlangs->convToOutputCharset($newvalue);
352 if (preg_match(
'/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/i', $newvalue)) {
354 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
355 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
356 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd');
357 } 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)) {
359 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
360 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
361 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd h:mm:ss');
363 if ($typefield ==
'Text' || $typefield ==
'TextAuto') {
364 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, (
string) $newvalue);
365 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
366 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'@');
367 $this->workbook->getActiveSheet()->getStyle($coord)->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
369 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
374 foreach ($selectlabelvalues as $key => $newvalue) {
375 if (preg_match(
'/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/i', $newvalue)) {
377 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
378 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
379 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd');
380 } 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)) {
382 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
383 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
384 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd h:mm:ss');
386 if ($typefield ==
'Text' || $typefield ==
'TextAuto') {
387 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, (
string) $newvalue);
388 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
389 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'@');
390 $this->workbook->getActiveSheet()->getStyle($coord)->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
392 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
427 $objWriter =
new Xlsx($this->workbook);
428 $objWriter->save($this->file);
429 $this->workbook->disconnectWorksheets();
430 unset($this->workbook);
469 $c = intval(($c - $p) / 26);
470 $letter = chr(65 + $p).$letter;
487 $this->workbook->getActiveSheet()->setCellValue($startCell, $val);
489 if (!empty($endCell)) {
490 $cellRange = $startCell.
':'.$endCell;
491 $this->workbook->getActiveSheet()->mergeCells($startCell.
':'.$endCell);
493 $cellRange = $startCell;
495 if (!empty($this->styleArray)) {
496 $this->workbook->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->styleArray);
499 $this->error = $e->getMessage();
514 $this->styleArray[
'borders'] = array(
516 'borderStyle' => $thickness,
517 'color' => array(
'argb' => $color)
532 $this->styleArray[
'font'] = array(
533 'color' => array(
'argb' => $color),
547 $this->styleArray[
'alignment'] = array(
'horizontal' => $horizontal);
557 $this->styleArray = array();
569 public function setBlock($startCell, $TDatas = array(), $boldTitle =
false)
572 if (!empty($TDatas)) {
573 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
574 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
575 $startRow = $startCell->getRow();
576 foreach ($TDatas as $column => $TRows) {
578 $this->
setFontStyle(
true, $this->styleArray[
'font'][
'color'][
'argb']);
580 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
584 $this->
setFontStyle(
false, $this->styleArray[
'font'][
'color'][
'argb']);
586 foreach ($TRows as $row) {
588 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $rowPos);
595 $this->error = $e->getMessage();
612 if (!empty($TDatas)) {
613 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
614 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
615 $startRow = $startCell->getRow();
616 foreach ($TDatas as $title => $val) {
617 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
619 $this->
setFontStyle(
true, $this->styleArray[
'font'][
'color'][
'argb']);
623 $this->
setFontStyle(
false, $this->styleArray[
'font'][
'color'][
'argb']);
625 $cell2 = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + 1, $startRow);
631 $this->error = $e->getMessage();
646 foreach (range($firstColumn, $lastColumn) as $columnID) {
647 $this->workbook->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(
true);
664 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
665 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
666 if (!empty($offset)) {
667 $startColumn += $offset;
670 $startRow = $startCell->getRow();
671 $startCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
672 $startCoordinate = $startCell->getCoordinate();
675 $endCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + ($length - 1), $startRow);
676 $endCoordinate = $endCell->getCoordinate();
677 $this->workbook->getActiveSheet()->mergeCells($startCoordinate.
':'.$endCoordinate);
679 $this->error = $e->getMessage();
682 return $endCoordinate;
Class to build export files with Excel format.
write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
Output record line into file.
setBlock2Columns($startCell, $TDatas=array(), $boldTitle=false)
Make a 2xN Tab in Sheet.
getDriverVersion()
getDriverVersion
setCellValue($val, $startCell, $endCell='')
Set cell value and automatically merge if we give an endcell.
getLibVersion()
getLibVersion
setMergeCellValueByLength($val, $startCell, $length, $offset=0)
Set a value cell and merging it by giving a starting cell and a length.
getDriverDesc()
getDriverDesc
close_file()
Close Excel file.
write_footer($outputlangs)
Write footer.
column2Letter($c)
Convert a column to letter (1->A, 0->B, 27->AA, ...)
getDriverExtension()
getDriverExtension
__construct($db)
Constructor.
setFontStyle($bold, $color)
Set font style.
setBlock($startCell, $TDatas=array(), $boldTitle=false)
Make a NxN Block in sheet.
open_file($file, $outputlangs)
Open output file.
write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
Output title line into file.
enableAutosize($firstColumn, $lastColumn)
Enable auto sizing for column range.
setAlignmentStyle($horizontal)
Set alignment style (horizontal, left, right, ...)
write_header($outputlangs)
Write header.
getDriverLabel()
getDriverLabel
excel_clean($newvalue)
Clean a cell to respect rules of Excel file cells.
setBorderStyle($thickness, $color)
Set border style.
Parent class for export modules.
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...
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
jsonOrUnserialize($stringtodecode)
Decode an encode string.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.