204 global $user, $langs;
206 dol_syslog(get_class($this).
"::open_file file=".$file);
211 $outputlangs->load(
"exports");
213 require_once DOL_DOCUMENT_ROOT.
'/includes/phpoffice/phpspreadsheet/src/autoloader.php';
214 require_once DOL_DOCUMENT_ROOT.
'/includes/Psr/autoloader.php';
215 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
217 if ($this->
id ==
'excel2007') {
218 if (!class_exists(
'ZipArchive')) {
219 $langs->load(
"errors");
220 $this->error = $langs->trans(
'ErrorPHPNeedModule',
'zip');
225 $this->workbook =
new Spreadsheet();
226 $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs).
' - '.DOL_APPLICATION_TITLE.
' '.DOL_VERSION);
228 $this->workbook->getProperties()->setTitle(basename($file));
229 $this->workbook->getProperties()->setSubject(basename($file));
230 $this->workbook->getProperties()->setDescription(DOL_APPLICATION_TITLE.
' '.DOL_VERSION);
232 $this->workbook->setActiveSheetIndex(0);
233 $this->workbook->getActiveSheet()->setTitle($outputlangs->trans(
"Sheet"));
234 $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(16);
265 public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
270 $this->workbook->getActiveSheet()->getStyle(
'1')->getFont()->setBold(
true);
271 $this->workbook->getActiveSheet()->getStyle(
'1')->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
272 $selectlabel = array();
276 foreach ($array_selected_sorted as $code => $value) {
277 $alias = $array_export_fields_label[$code];
280 dol_syslog(
'Bad value for field with code='.$code.
'. Try to redefine export.', LOG_WARNING);
283 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
285 if (preg_match(
'/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
286 $selectlabel[$code.
"_label"] = $alias.
"_label";
288 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $outputlangs->transnoentities($alias));
289 if (!empty($array_types[$code]) && in_array($array_types[$code], array(
'Date',
'Numeric',
'TextAuto'))) {
290 $this->workbook->getActiveSheet()->getColumnDimension($this->
column2Letter($this->col + 1))->setAutoSize(
true);
296 foreach ($selectlabel as $key => $value) {
297 $code = preg_replace(
'/_label$/',
'', $key);
298 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $outputlangs->transnoentities($value));
299 if (!empty($array_types[$code]) && in_array($array_types[$code], array(
'Date',
'Numeric',
'TextAuto'))) {
300 $this->workbook->getActiveSheet()->getColumnDimension($this->
column2Letter($this->col + 1))->setAutoSize(
true);
319 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
327 $selectlabelvalues = array();
328 foreach ($array_selected_sorted as $code => $value) {
329 if (strpos($code,
' as ') == 0) {
330 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $code);
332 $alias = substr($code, strpos($code,
' as ') + 4);
335 dol_syslog(
'Bad value for field with code='.$code.
'. Try to redefine export.', LOG_WARNING);
339 $newvalue = !empty($objp->$alias) ? $objp->$alias :
'';
342 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
344 if (preg_match(
'/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
346 if (is_array($array) && !empty($newvalue)) {
347 $array = $array[
'options'];
348 $selectlabelvalues[$code.
"_label"] = $array[$newvalue];
350 $selectlabelvalues[$code.
"_label"] =
"";
355 if (preg_match(
'/^\((.*)\)$/i', $newvalue, $reg)) {
356 $newvalue = $outputlangs->transnoentities($reg[1]);
358 $newvalue = $outputlangs->convToOutputCharset($newvalue);
361 if (preg_match(
'/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/i', $newvalue)) {
363 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
364 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
365 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd');
366 } 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)) {
368 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
369 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
370 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd h:mm:ss');
372 if ($typefield ==
'Text' || $typefield ==
'TextAuto') {
375 $newvalue = (
dol_substr($newvalue, 0, 1) ===
'=' ?
'\'' :
'') . $newvalue;
376 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
377 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
378 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'@');
379 $this->workbook->getActiveSheet()->getStyle($coord)->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
381 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
388 foreach ($selectlabelvalues as $key => $newvalue) {
389 $code = preg_replace(
'/_label$/',
'', $key);
390 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
392 if (preg_match(
'/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/i', $newvalue)) {
394 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
395 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
396 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd');
397 } 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)) {
399 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
400 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
401 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd h:mm:ss');
403 if ($typefield ==
'Text' || $typefield ==
'TextAuto') {
404 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, (
string) $newvalue);
405 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
406 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'@');
407 $this->workbook->getActiveSheet()->getStyle($coord)->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
409 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
586 public function setBlock($startCell, $TDatas = array(), $boldTitle =
false)
589 if (!empty($TDatas)) {
590 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
591 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
592 $startRow = $startCell->getRow();
593 foreach ($TDatas as $column => $TRows) {
595 $this->
setFontStyle(
true, $this->styleArray[
'font'][
'color'][
'argb']);
597 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
601 $this->
setFontStyle(
false, $this->styleArray[
'font'][
'color'][
'argb']);
603 foreach ($TRows as $row) {
605 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $rowPos);
612 $this->error = $e->getMessage();
629 if (!empty($TDatas)) {
630 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
631 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
632 $startRow = $startCell->getRow();
633 foreach ($TDatas as $title => $val) {
634 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
636 $this->
setFontStyle(
true, $this->styleArray[
'font'][
'color'][
'argb']);
640 $this->
setFontStyle(
false, $this->styleArray[
'font'][
'color'][
'argb']);
642 $cell2 = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + 1, $startRow);
648 $this->error = $e->getMessage();
681 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
682 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
683 if (!empty($offset)) {
684 $startColumn += $offset;
687 $startRow = $startCell->getRow();
688 $startCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
689 $startCoordinate = $startCell->getCoordinate();
692 $endCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + ($length - 1), $startRow);
693 $endCoordinate = $endCell->getCoordinate();
694 $this->workbook->getActiveSheet()->mergeCells($startCoordinate.
':'.$endCoordinate);
696 $this->error = $e->getMessage();
699 return $endCoordinate;