27 require_once DOL_DOCUMENT_ROOT.
'/core/modules/export/modules_export.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
30 use PhpOffice\PhpSpreadsheet\Spreadsheet;
31 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
32 use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
55 public $version =
'dolibarr';
85 $this->
id =
'excel2007';
86 $this->label =
'Excel 2007';
87 $this->desc = $langs->trans(
'Excel2007FormatDesc');
88 $this->extension =
'xlsx';
89 $this->picto =
'mime/xls';
90 $this->version =
'1.30';
91 $this->phpmin = array(7, 0);
96 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
97 $this->label_lib =
'PhpSpreadSheet';
98 $this->version_lib =
'1.12.0';
132 $langs->load(
"errors");
133 return $langs->trans(
"NumberOfLinesLimited");
153 return $this->extension;
163 return $this->version;
173 return $this->label_lib;
183 return $this->version_lib;
198 global $user, $langs;
200 dol_syslog(get_class($this).
"::open_file file=".$file);
205 $outputlangs->load(
"exports");
207 require_once DOL_DOCUMENT_ROOT.
'/includes/phpoffice/phpspreadsheet/src/autoloader.php';
208 require_once DOL_DOCUMENT_ROOT.
'/includes/Psr/autoloader.php';
209 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
211 if ($this->
id ==
'excel2007') {
212 if (!class_exists(
'ZipArchive')) {
213 $langs->load(
"errors");
214 $this->error = $langs->trans(
'ErrorPHPNeedModule',
'zip');
219 $this->workbook =
new Spreadsheet();
220 $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs).
' - '.DOL_APPLICATION_TITLE.
' '.DOL_VERSION);
222 $this->workbook->getProperties()->setTitle(basename($file));
223 $this->workbook->getProperties()->setSubject(basename($file));
224 $this->workbook->getProperties()->setDescription(DOL_APPLICATION_TITLE.
' '.DOL_VERSION);
226 $this->workbook->setActiveSheetIndex(0);
227 $this->workbook->getActiveSheet()->setTitle($outputlangs->trans(
"Sheet"));
228 $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(16);
259 public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
264 $this->workbook->getActiveSheet()->getStyle(
'1')->getFont()->setBold(
true);
265 $this->workbook->getActiveSheet()->getStyle(
'1')->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
266 $selectlabel = array();
270 foreach ($array_selected_sorted as $code => $value) {
271 $alias = $array_export_fields_label[$code];
274 dol_print_error(
null,
'Bad value for field with code='.$code.
'. Try to redefine export.');
276 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
278 if (preg_match(
'/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
279 $selectlabel[$code.
"_label"] = $alias.
"_label";
281 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $outputlangs->transnoentities($alias));
282 if (!empty($array_types[$code]) && in_array($array_types[$code], array(
'Date',
'Numeric',
'TextAuto'))) {
283 $this->workbook->getActiveSheet()->getColumnDimension($this->
column2Letter($this->col + 1))->setAutoSize(
true);
289 foreach ($selectlabel as $key => $value) {
290 $code = preg_replace(
'/_label$/',
'', $key);
291 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $outputlangs->transnoentities($value));
292 if (!empty($array_types[$code]) && in_array($array_types[$code], array(
'Date',
'Numeric',
'TextAuto'))) {
293 $this->workbook->getActiveSheet()->getColumnDimension($this->
column2Letter($this->col + 1))->setAutoSize(
true);
312 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
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(
null,
'Bad value for field with code='.$code.
'. Try to redefine export.');
331 $newvalue = !empty($objp->$alias) ? $objp->$alias :
'';
334 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
336 if (preg_match(
'/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
338 if (is_array($array) && !empty($newvalue)) {
339 $array = $array[
'options'];
340 $selectlabelvalues[$code.
"_label"] = $array[$newvalue];
342 $selectlabelvalues[$code.
"_label"] =
"";
347 if (preg_match(
'/^\((.*)\)$/i', $newvalue, $reg)) {
348 $newvalue = $outputlangs->transnoentities($reg[1]);
350 $newvalue = $outputlangs->convToOutputCharset($newvalue);
353 if (preg_match(
'/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/i', $newvalue)) {
355 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
356 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
357 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd');
358 } 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)) {
360 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
361 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
362 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd h:mm:ss');
364 if ($typefield ==
'Text' || $typefield ==
'TextAuto') {
367 $newvalue = (
dol_substr($newvalue, 0, 1) ===
'=' ?
'\'' :
'') . $newvalue;
368 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
369 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
370 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'@');
371 $this->workbook->getActiveSheet()->getStyle($coord)->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
373 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
380 foreach ($selectlabelvalues as $key => $newvalue) {
381 $code = preg_replace(
'/_label$/',
'', $key);
382 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
384 if (preg_match(
'/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/i', $newvalue)) {
386 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
387 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
388 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd');
389 } 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)) {
391 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
392 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
393 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd h:mm:ss');
395 if ($typefield ==
'Text' || $typefield ==
'TextAuto') {
396 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, (
string) $newvalue);
397 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
398 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'@');
399 $this->workbook->getActiveSheet()->getStyle($coord)->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
401 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
436 $objWriter =
new Xlsx($this->workbook);
437 $objWriter->save($this->file);
438 $this->workbook->disconnectWorksheets();
439 unset($this->workbook);
478 $c = intval(($c - $p) / 26);
479 $letter = chr(65 + $p).$letter;
496 $this->workbook->getActiveSheet()->setCellValue($startCell, $val);
498 if (!empty($endCell)) {
499 $cellRange = $startCell.
':'.$endCell;
500 $this->workbook->getActiveSheet()->mergeCells($startCell.
':'.$endCell);
502 $cellRange = $startCell;
504 if (!empty($this->styleArray)) {
505 $this->workbook->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->styleArray);
508 $this->error = $e->getMessage();
523 $this->styleArray[
'borders'] = array(
525 'borderStyle' => $thickness,
526 'color' => array(
'argb' => $color)
541 $this->styleArray[
'font'] = array(
542 'color' => array(
'argb' => $color),
556 $this->styleArray[
'alignment'] = array(
'horizontal' => $horizontal);
566 $this->styleArray = array();
578 public function setBlock($startCell, $TDatas = array(), $boldTitle =
false)
581 if (!empty($TDatas)) {
582 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
583 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
584 $startRow = $startCell->getRow();
585 foreach ($TDatas as $column => $TRows) {
587 $this->
setFontStyle(
true, $this->styleArray[
'font'][
'color'][
'argb']);
589 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
593 $this->
setFontStyle(
false, $this->styleArray[
'font'][
'color'][
'argb']);
595 foreach ($TRows as $row) {
597 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $rowPos);
604 $this->error = $e->getMessage();
621 if (!empty($TDatas)) {
622 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
623 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
624 $startRow = $startCell->getRow();
625 foreach ($TDatas as $title => $val) {
626 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
628 $this->
setFontStyle(
true, $this->styleArray[
'font'][
'color'][
'argb']);
632 $this->
setFontStyle(
false, $this->styleArray[
'font'][
'color'][
'argb']);
634 $cell2 = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + 1, $startRow);
640 $this->error = $e->getMessage();
655 foreach (range($firstColumn, $lastColumn) as $columnID) {
656 $this->workbook->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(
true);
673 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
674 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
675 if (!empty($offset)) {
676 $startColumn += $offset;
679 $startRow = $startCell->getRow();
680 $startCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
681 $startCoordinate = $startCell->getCoordinate();
684 $endCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + ($length - 1), $startRow);
685 $endCoordinate = $endCell->getCoordinate();
686 $this->workbook->getActiveSheet()->mergeCells($startCoordinate.
':'.$endCoordinate);
688 $this->error = $e->getMessage();
691 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
getDriverLabelBis()
getDriverLabel
__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_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
dol_print_error($db=null, $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.