Beispiel #1
0
 /**
  * @inheritDoc
  */
 public function init()
 {
     parent::init();
     foreach ($this->columns as $key => $column) {
         if (is_array($column)) {
             $this->columns[$key] = \Yii::createObject(ArrayHelper::merge(['activeDataProvider' => $this->activeDataProvider], $column));
         }
     }
     $this->activeDataProvider->pagination = ['defaultPageSize' => false, 'pageSizeLimit' => false];
     $this->reader = \PHPExcel_IOFactory::createReader(\PHPExcel_IOFactory::identify($this->filename));
     $this->reader->setReadDataOnly(true);
     $this->phpExcel = $this->reader->load($this->filename);
     $this->models = ArrayHelper::map($this->activeDataProvider->getModels(), 'primaryKey', function ($item) {
         return $item;
     });
 }
 public function __construct(\SplFileObject $file, $headerRowNumber = null, $activeSheet = null, $readOnly = true, $maxRows = null, $maxCol = null)
 {
     $this->file = $file;
     $this->activeSheet = $activeSheet;
     $this->reader = \PHPExcel_IOFactory::createReaderForFile($file->getPathName());
     $this->reader->setReadDataOnly($readOnly);
     if (!is_null($headerRowNumber)) {
         $this->headerRowNumber = $headerRowNumber;
         $headerReader = clone $this->reader;
         $headerReader->setReadFilter(new ReadFilter($headerRowNumber + 1));
         /** @var \PHPExcel $excel */
         $excel = $headerReader->load($file->getPathname());
         if (null !== $activeSheet) {
             $excel->setActiveSheetIndex($activeSheet);
         }
         $rows = $excel->getActiveSheet()->toArray();
         $this->columnHeaders = $rows[$headerRowNumber];
         //set max col from header length if not already given
         if (is_null($maxCol)) {
             $maxCol = \PHPExcel_Cell::stringFromColumnIndex(count($this->columnHeaders) - 1);
         }
     }
     $this->setBoundaries($maxRows, $maxCol);
 }
Beispiel #3
0
 /**
  * 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;
 }