30use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
31use PhpOffice\PhpSpreadsheet\Spreadsheet;
32use PhpOffice\PhpSpreadsheet\Style\Alignment;
33use PhpOffice\PhpSpreadsheet\Shared\Date;
35require_once DOL_DOCUMENT_ROOT .
'/core/modules/import/modules_import.class.php';
57 public $version =
'dolibarr';
84 public $cacheconvert = array();
86 public $cachefieldtable = array();
118 parent::__construct();
123 $this->label =
'Excel 2007';
124 $this->desc = $langs->trans(
"Excel2007FormatDesc");
125 $this->extension =
'xlsx';
126 $this->picto =
'mime/xls';
127 $this->version =
'1.0';
128 $this->phpmin = array(7, 1);
130 require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
132 dol_syslog(
"Module need a higher PHP version");
133 $this->error =
"Module need a higher PHP version";
138 require_once DOL_DOCUMENT_ROOT.
'/includes/phpoffice/phpspreadsheet/src/autoloader.php';
139 require_once DOL_DOCUMENT_ROOT.
'/includes/Psr/autoloader.php';
140 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
141 $this->workbook =
new Spreadsheet();
144 if (!class_exists(
'ZipArchive')) {
145 $langs->load(
"errors");
146 $this->error = $langs->trans(
'ErrorPHPNeedModule',
'zip');
149 $this->label_lib =
'PhpSpreadSheet';
150 $this->version_lib =
'1.8.0';
152 $arrayofstreams = stream_get_wrappers();
153 if (!in_array(
'zip', $arrayofstreams)) {
154 $langs->load(
"errors");
155 $this->error = $langs->trans(
'ErrorStreamMustBeEnabled',
'zip');
159 $this->datatoimport = $datatoimport;
160 if (preg_match(
'/^societe_/', $datatoimport)) {
161 $this->thirdpartyobject =
new Societe($this->db);
176 global $user,
$conf, $langs, $file;
178 $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs) .
' - Dolibarr ' . DOL_VERSION);
179 $this->workbook->getProperties()->setTitle($outputlangs->trans(
"Import") .
' - ' . $file);
180 $this->workbook->getProperties()->setSubject($outputlangs->trans(
"Import") .
' - ' . $file);
181 $this->workbook->getProperties()->setDescription($outputlangs->trans(
"Import") .
' - ' . $file);
183 $this->workbook->setActiveSheetIndex(0);
184 $this->workbook->getActiveSheet()->setTitle($outputlangs->trans(
"Sheet"));
185 $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(16);
202 $this->workbook->getActiveSheet()->getStyle(
'1')->getFont()->setBold(
true);
203 $this->workbook->getActiveSheet()->getStyle(
'1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
206 foreach ($headerlinefields as $field) {
207 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, 1, $outputlangs->transnoentities($field));
229 foreach ($contentlinevalues as $cell) {
230 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, $row, $cell);
248 $tempfile = tempnam(sys_get_temp_dir(),
'dol');
249 $objWriter =
new PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->workbook);
250 $objWriter->save($tempfile);
251 $this->workbook->disconnectWorksheets();
252 unset($this->workbook);
254 $content = file_get_contents($tempfile);
273 dol_syslog(get_class($this) .
"::open_file file=" . $file);
275 $reader =
new Xlsx();
276 $this->workbook = $reader->load($file);
294 $reader =
new Xlsx();
295 $this->workbook = $reader->load($file);
297 $rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
299 $this->workbook->disconnectWorksheets();
300 unset($this->workbook);
316 $this->headers = array();
318 $info = $xlsx->listWorksheetinfo($this->file);
319 $countcolumns = $info[0][
'totalColumns'];
320 for ($col = 1; $col <= $countcolumns; $col++) {
321 $this->headers[$col] = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, 1)->getValue();
336 $rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
337 if ($this->record > $rowcount) {
343 $info = $xlsx->listWorksheetinfo($this->file);
344 $countcolumns = $info[0][
'totalColumns'];
346 for ($col = 1; $col <= $countcolumns; $col++) {
347 $tmpcell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, $this->record);
349 $val = $tmpcell->getValue();
351 if (Date::isDateTime($tmpcell)) {
353 $dateValue = Date::excelToDateTimeObject($val);
354 $val = $dateValue->format(
'Y-m-d H:i:s');
357 $array[$col][
'val'] = trim($val);
358 $array[$col][
'type'] = (
dol_strlen($val) ? 1 : -1);
376 $this->workbook->disconnectWorksheets();
377 unset($this->workbook);
393 public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
396 return $this->
commonImportInsert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys, 1);
versionphparray()
Return version PHP.
versioncompare($versionarray1, $versionarray2)
Compare 2 versions (stored into 2 arrays), to know if a version (a,b,c) is lower than (x,...
Class to import Excel files.
__construct($db, $datatoimport)
Constructor.
write_record_example($outputlangs, $contentlinevalues)
Output record of an example file for this format.
import_open_file($file)
Open input file.
import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
Insert a record into database.
write_footer_example($outputlangs)
Output footer of an example file for this format.
import_read_header()
Input header line from file.
write_header_example($outputlangs)
Output header of an example file for this format.
import_read_record()
Return array of next record in input file.
write_title_example($outputlangs, $headerlinefields)
Output title line of an example file for this format.
import_get_nb_of_lines($file)
Return nb of records.
import_close_file()
Close file handle.
Parent class for import file readers.
commonImportInsert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys, $recordpositionbase=0)
Shared implementation of import_insert for CSV/XLSX.
Class to manage third parties objects (customers, suppliers, prospects...)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.