public function __construct($fileName, $csvDelimiter = null, $csvEnclosure = null, $sheetNumber = 1, $chunkSize = null, $fillMissingColumns = true, $removeCellsNotMatchingHeaderColumns = true) { $this->filename = $fileName; $this->sheetNumber = $sheetNumber; $this->fillMissingColumns = $fillMissingColumns; $this->removeCellsNotMatchingHeaderColumns = $removeCellsNotMatchingHeaderColumns; //@todo: Allow disabling chunked read $this->filter = new ChunkReadFilter(0, $chunkSize); $this->reader = \PHPExcel_IOFactory::createReaderForFile($this->filename); if ($this->reader instanceof \PHPExcel_Reader_CSV) { if ($csvDelimiter) { $this->reader->setDelimiter($csvDelimiter); } if ($csvEnclosure) { $this->reader->setEnclosure($csvEnclosure); } } $this->reader->setReadFilter($this->filter); $worksheet = $this->reloadIterator(1); $this->highestDataColumnName = $worksheet->getHighestDataColumn(); $this->highestRow = $worksheet->getHighestRow(); $this->rowIterator->rewind(); $this->header = $this->getFilteredArrayForCurrentRow(); foreach ($this->header as $headerIndex => &$columnHeader) { if (is_null($columnHeader) || $columnHeader === '') { $columnHeader = $headerIndex; } else { $columnHeader = trim($columnHeader); } } $this->rowIterator->next(); }
/** * Load an existing file in memory in order to write into it and instantiate PhpExcel object. * * @param string $filename Chemin du fichier à charger * @param null|string $fileType Type de fichier (ou null si la confiance règne). * * @return $this * @throws CannotReadFileException * @throws \PHPExcel_Reader_Exception */ public function loadFile($filename, $fileType = null) { if (null !== $fileType) { $this->fileType = $fileType; } else { $this->fileType = \PHPExcel_IOFactory::identify($filename); } if (!is_readable($filename)) { throw new CannotReadFileException($filename); } $this->filename = $filename; $this->reader = \PHPExcel_IOFactory::createReader($this->fileType); if ($this->fileType !== 'CSV') { $this->reader->setReadDataOnly(false); } else { $this->reader->setDelimiter(';'); } $this->objPHPExcel = $this->reader->load($this->filename); $this->countRows(); $this->rowIndex = 1; $this->isLoaded = true; return $this; }