/** * Loads PHPExcel from file into PHPExcel instance * * @param string $pFilename * @param PHPExcel $objPHPExcel * @return PHPExcel * @throws Exception */ public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel) { // Check if file exists if (!file_exists($pFilename)) { throw new Exception("Could not open " . $pFilename . " for reading! File does not exist."); } // Create new PHPExcel while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) { $objPHPExcel->createSheet(); } $objPHPExcel->setActiveSheetIndex($this->_sheetIndex); // Open file $fileHandle = fopen($pFilename, 'r'); if ($fileHandle === false) { throw new Exception("Could not open file {$pFilename} for reading."); } // Skip BOM, if any switch ($this->_inputEncoding) { case 'UTF-8': fgets($fileHandle, 4) == "" ? fseek($fileHandle, 3) : fseek($fileHandle, 0); break; default: break; } // Loop through file $currentRow = 0; $rowData = array(); while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) { ++$currentRow; $rowDataCount = count($rowData); for ($i = 0; $i < $rowDataCount; ++$i) { $columnLetter = PHPExcel_Cell::stringFromColumnIndex($i); if ($rowData[$i] != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) { // Unescape enclosures $rowData[$i] = str_replace("\\" . $this->_enclosure, $this->_enclosure, $rowData[$i]); $rowData[$i] = str_replace($this->_enclosure . $this->_enclosure, $this->_enclosure, $rowData[$i]); // Convert encoding if necessary if ($this->_inputEncoding !== 'UTF-8') { $rowData[$i] = PHPExcel_Shared_String::ConvertEncoding($rowData[$i], 'UTF-8', $this->_inputEncoding); } // Set cell value $objPHPExcel->getActiveSheet()->setCellValue($columnLetter . $currentRow, $rowData[$i]); } } } // Close file fclose($fileHandle); // Return return $objPHPExcel; }
/** * Loads PHPExcel from file into PHPExcel instance * * @param string $pFilename * @param PHPExcel $objPHPExcel * @throws Exception */ public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel) { // Check if file exists if (!file_exists($pFilename)) { throw new Exception("Could not open " . $pFilename . " for reading! File does not exist."); } // Create new PHPExcel while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) { $objPHPExcel->createSheet(); } $objPHPExcel->setActiveSheetIndex($this->_sheetIndex); // Open file $fileHandle = fopen($pFilename, 'r'); if ($fileHandle === false) { throw new Exception("Could not open file {$pFilename} for reading."); } // Loop trough file $currentRow = 0; $rowData = array(); while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) { ++$currentRow; $rowDataCount = count($rowData); for ($i = 0; $i < $rowDataCount; ++$i) { $columnLetter = PHPExcel_Cell::stringFromColumnIndex($i); if ($rowData[$i] != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) { // Unescape enclosures $rowData[$i] = str_replace("\\" . $this->_enclosure, $this->_enclosure, $rowData[$i]); $rowData[$i] = str_replace($this->_enclosure . $this->_enclosure, $this->_enclosure, $rowData[$i]); // Set cell value $objPHPExcel->getActiveSheet()->setCellValue($columnLetter . $currentRow, $rowData[$i]); } } } // Close file fclose($fileHandle); // Return return $objPHPExcel; }
/** * Enable filters * * @return $this */ protected function _enableFilters() { // Loop through the registered filters foreach ($this->filters['registered'] as $key => $class) { // Set the filter inside the reader when enabled and the class exists if (in_array($key, $this->filters['enabled']) && class_exists($class)) { // init new filter (and overrule the current) $this->filter = new $class(); // Set default rows if (method_exists($this->filter, 'setRows')) { $this->filter->setRows(0, 1); } // Set the read filter $this->reader->setReadFilter($this->filter); } } return $this; }
/** * Loads PHPExcel from file into PHPExcel instance * * @access public * @param string $pFilename * @param PHPExcel $objPHPExcel * @return PHPExcel * @throws Exception */ public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel) { // Check if file exists if (!file_exists($pFilename)) { throw new Exception("Could not open " . $pFilename . " for reading! File does not exist."); } // Create new PHPExcel while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) { $objPHPExcel->createSheet(); } $objPHPExcel->setActiveSheetIndex($this->_sheetIndex); // Open file $fileHandle = fopen($pFilename, 'r'); if ($fileHandle === false) { throw new Exception("Could not open file {$pFilename} for reading."); } // Skip BOM, if any switch ($this->_inputEncoding) { case 'UTF-8': fgets($fileHandle, 4) == "" ? fseek($fileHandle, 3) : fseek($fileHandle, 0); break; case 'UTF-16LE': fgets($fileHandle, 3) == "ÿþ" ? fseek($fileHandle, 2) : fseek($fileHandle, 0); break; case 'UTF-16BE': fgets($fileHandle, 3) == "þÿ" ? fseek($fileHandle, 2) : fseek($fileHandle, 0); break; case 'UTF-32LE': fgets($fileHandle, 5) == "ÿþ" ? fseek($fileHandle, 4) : fseek($fileHandle, 0); break; case 'UTF-32BE': fgets($fileHandle, 5) == "þÿ" ? fseek($fileHandle, 4) : fseek($fileHandle, 0); break; default: break; } $escapeEnclosures = array("\\" . $this->_enclosure, $this->_enclosure . $this->_enclosure); // Set our starting row based on whether we're in contiguous mode or not $currentRow = 1; if ($this->_contiguous) { $currentRow = $this->_contiguousRow == -1 ? $objPHPExcel->getActiveSheet()->getHighestRow() : $this->_contiguousRow; } // Loop through each line of the file in turn while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) { $columnLetter = 'A'; foreach ($rowData as $rowDatum) { if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) { // Unescape enclosures $rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum); // Convert encoding if necessary if ($this->_inputEncoding !== 'UTF-8') { $rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding); } // Set cell value $objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowDatum); } ++$columnLetter; } ++$currentRow; } // Close file fclose($fileHandle); if ($this->_contiguous) { $this->_contiguousRow = $currentRow; } // Return return $objPHPExcel; }