196 global $user, $conf, $langs;
198 dol_syslog(get_class($this).
"::open_file file=".$file);
203 $outputlangs->load(
"exports");
205 require_once DOL_DOCUMENT_ROOT.
'/includes/phpoffice/phpspreadsheet/src/autoloader.php';
206 require_once DOL_DOCUMENT_ROOT.
'/includes/Psr/autoloader.php';
207 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
209 if ($this->
id ==
'excel2007') {
210 if (!class_exists(
'ZipArchive')) {
211 $langs->load(
"errors");
212 $this->error = $langs->trans(
'ErrorPHPNeedModule',
'zip');
217 $this->workbook =
new Spreadsheet();
218 $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs).
' - '.DOL_APPLICATION_TITLE.
' '.DOL_VERSION);
220 $this->workbook->getProperties()->setTitle(basename($file));
221 $this->workbook->getProperties()->setSubject(basename($file));
222 $this->workbook->getProperties()->setDescription(DOL_APPLICATION_TITLE.
' '.DOL_VERSION);
224 $this->workbook->setActiveSheetIndex(0);
225 $this->workbook->getActiveSheet()->setTitle($outputlangs->trans(
"Sheet"));
226 $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(16);
257 public function write_title($array_export_fields_label, $array_selected_sorted, $outputlangs, $array_types)
263 $this->workbook->getActiveSheet()->getStyle(
'1')->getFont()->setBold(
true);
264 $this->workbook->getActiveSheet()->getStyle(
'1')->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
265 $selectlabel = array();
269 foreach ($array_selected_sorted as $code => $value) {
270 $alias = $array_export_fields_label[$code];
273 dol_print_error(
'',
'Bad value for field with code='.$code.
'. Try to redefine export.');
275 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
277 if (preg_match(
'/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
278 $selectlabel[$code.
"_label"] = $alias.
"_label";
280 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $outputlangs->transnoentities($alias));
281 if (!empty($array_types[$code]) && in_array($array_types[$code], array(
'Date',
'Numeric',
'TextAuto'))) {
282 $this->workbook->getActiveSheet()->getColumnDimension($this->
column2Letter($this->col + 1))->setAutoSize(
true);
286 foreach ($selectlabel as $key => $value) {
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);
307 public function write_record($array_selected_sorted, $objp, $outputlangs, $array_types)
316 $selectlabelvalues = array();
317 foreach ($array_selected_sorted as $code => $value) {
318 if (strpos($code,
' as ') == 0) {
319 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $code);
321 $alias = substr($code, strpos($code,
' as ') + 4);
324 dol_print_error(
'',
'Bad value for field with code='.$code.
'. Try to redefine export.');
326 $newvalue = $objp->$alias;
329 $typefield = isset($array_types[$code]) ? $array_types[$code] :
'';
331 if (preg_match(
'/^Select:/i', $typefield) && $typefield = substr($typefield, 7)) {
333 if (is_array($array) && !empty($newvalue)) {
334 $array = $array[
'options'];
335 $selectlabelvalues[$code.
"_label"] = $array[$newvalue];
337 $selectlabelvalues[$code.
"_label"] =
"";
342 if (preg_match(
'/^\((.*)\)$/i', $newvalue, $reg)) {
343 $newvalue = $outputlangs->transnoentities($reg[1]);
345 $newvalue = $outputlangs->convToOutputCharset($newvalue);
348 if (preg_match(
'/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/i', $newvalue)) {
350 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
351 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
352 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd');
353 } 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)) {
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 h:mm:ss');
359 if ($typefield ==
'Text' || $typefield ==
'TextAuto') {
362 $newvalue = (
dol_substr($newvalue, 0, 1) ===
'=' ?
'\'' :
'') . $newvalue;
363 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
364 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
365 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'@');
366 $this->workbook->getActiveSheet()->getStyle($coord)->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
368 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
373 foreach ($selectlabelvalues as $key => $newvalue) {
374 if (preg_match(
'/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/i', $newvalue)) {
376 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
377 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
378 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd');
379 } 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)) {
381 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($newvalue));
382 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
383 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'yyyy-mm-dd h:mm:ss');
385 if ($typefield ==
'Text' || $typefield ==
'TextAuto') {
386 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, (
string) $newvalue);
387 $coord = $this->workbook->getActiveSheet()->getCellByColumnAndRow($this->col, $this->row + 1)->getCoordinate();
388 $this->workbook->getActiveSheet()->getStyle($coord)->getNumberFormat()->setFormatCode(
'@');
389 $this->workbook->getActiveSheet()->getStyle($coord)->getAlignment()->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
391 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($this->col, $this->row + 1, $newvalue);
568 public function setBlock($startCell, $TDatas = array(), $boldTitle =
false)
571 if (!empty($TDatas)) {
572 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
573 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
574 $startRow = $startCell->getRow();
575 foreach ($TDatas as $column => $TRows) {
577 $this->
setFontStyle(
true, $this->styleArray[
'font'][
'color'][
'argb']);
579 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
583 $this->
setFontStyle(
false, $this->styleArray[
'font'][
'color'][
'argb']);
585 foreach ($TRows as $row) {
587 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $rowPos);
594 $this->error = $e->getMessage();
611 if (!empty($TDatas)) {
612 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
613 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
614 $startRow = $startCell->getRow();
615 foreach ($TDatas as $title => $val) {
616 $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
618 $this->
setFontStyle(
true, $this->styleArray[
'font'][
'color'][
'argb']);
622 $this->
setFontStyle(
false, $this->styleArray[
'font'][
'color'][
'argb']);
624 $cell2 = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + 1, $startRow);
630 $this->error = $e->getMessage();
663 $startCell = $this->workbook->getActiveSheet()->getCell($startCell);
664 $startColumn = Coordinate::columnIndexFromString($startCell->getColumn());
665 if (!empty($offset)) {
666 $startColumn += $offset;
669 $startRow = $startCell->getRow();
670 $startCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow);
671 $startCoordinate = $startCell->getCoordinate();
674 $endCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + ($length - 1), $startRow);
675 $endCoordinate = $endCell->getCoordinate();
676 $this->workbook->getActiveSheet()->mergeCells($startCoordinate.
':'.$endCoordinate);
678 $this->error = $e->getMessage();
681 return $endCoordinate;