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();
110 public $countcolumns = 0;
123 parent::__construct();
128 $this->label =
'Excel 2007';
129 $this->desc = $langs->trans(
"Excel2007FormatDesc");
130 $this->extension =
'xlsx';
131 $this->picto =
'mime/xls';
132 $this->version =
'1.0';
133 $this->phpmin = array(7, 1);
135 require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
137 dol_syslog(
"Module need a higher PHP version");
138 $this->error =
"Module need a higher PHP version";
143 require_once DOL_DOCUMENT_ROOT.
'/includes/phpoffice/phpspreadsheet/src/autoloader.php';
144 require_once DOL_DOCUMENT_ROOT.
'/includes/Psr/autoloader.php';
145 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
146 $this->workbook =
new Spreadsheet();
149 if (!class_exists(
'ZipArchive')) {
150 $langs->load(
"errors");
151 $this->error = $langs->trans(
'ErrorPHPNeedModule',
'zip');
154 $this->label_lib =
'PhpSpreadSheet';
155 $this->version_lib =
'1.8.0';
157 $arrayofstreams = stream_get_wrappers();
158 if (!in_array(
'zip', $arrayofstreams)) {
159 $langs->load(
"errors");
160 $this->error = $langs->trans(
'ErrorStreamMustBeEnabled',
'zip');
164 $this->datatoimport = $datatoimport;
165 if (preg_match(
'/^societe_/', $datatoimport)) {
166 $this->thirdpartyobject =
new Societe($this->db);
181 global $user,
$conf, $langs, $file;
183 $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs) .
' - Dolibarr ' . DOL_VERSION);
184 $this->workbook->getProperties()->setTitle($outputlangs->trans(
"Import") .
' - ' . $file);
185 $this->workbook->getProperties()->setSubject($outputlangs->trans(
"Import") .
' - ' . $file);
186 $this->workbook->getProperties()->setDescription($outputlangs->trans(
"Import") .
' - ' . $file);
188 $this->workbook->setActiveSheetIndex(0);
189 $this->workbook->getActiveSheet()->setTitle($outputlangs->trans(
"Sheet"));
190 $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(16);
207 $this->workbook->getActiveSheet()->getStyle(
'1')->getFont()->setBold(
true);
208 $this->workbook->getActiveSheet()->getStyle(
'1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
211 foreach ($headerlinefields as $field) {
212 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, 1, $outputlangs->transnoentities($field));
234 foreach ($contentlinevalues as $cell) {
235 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, $row, $cell);
253 $tempfile = tempnam(sys_get_temp_dir(),
'dol');
254 $objWriter =
new PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->workbook);
255 $objWriter->save($tempfile);
256 $this->workbook->disconnectWorksheets();
257 unset($this->workbook);
259 $content = file_get_contents($tempfile);
278 dol_syslog(get_class($this) .
"::open_file file=" . $file);
280 $reader =
new Xlsx();
281 $this->workbook = $reader->load($file);
299 $reader =
new Xlsx();
300 $this->workbook = $reader->load($file);
302 $rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
304 $this->workbook->disconnectWorksheets();
305 unset($this->workbook);
321 $this->headers = array();
323 $info = $xlsx->listWorksheetinfo($this->file);
324 $countcolumns = $info[0][
'totalColumns'];
325 for ($col = 1; $col <= $countcolumns; $col++) {
326 $this->headers[$col] = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, 1)->getValue();
341 $rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
342 if ($this->record > $rowcount) {
347 if (empty($this->countcolumns)) {
349 $info = $xlsx->listWorksheetinfo($this->file);
350 $this->countcolumns = $info[0][
'totalColumns'];
353 $countcolumns = $this->countcolumns;
355 for ($col = 1; $col <= $countcolumns; $col++) {
356 $tmpcell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, $this->record);
358 $val = $tmpcell->getValue();
360 if (Date::isDateTime($tmpcell)) {
362 $dateValue = Date::excelToDateTimeObject($val);
363 $val = $dateValue->format(
'Y-m-d H:i:s');
366 $array[$col][
'val'] = trim($val);
367 $array[$col][
'type'] = (
dol_strlen($val) ? 1 : -1);
383 $this->workbook->disconnectWorksheets();
384 unset($this->workbook);
400 public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
403 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.