From c448f2ed7a57e06ad44d1473ae08c4b0b4079355 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sat, 23 Oct 2010 22:25:48 +0000 Subject: [PATCH] Implementation of the contiguous flag for the CSV reader, for use with a Read Filter git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@62697 2327b42d-5241-43d6-9e2a-de5ac946f064 --- Classes/PHPExcel/Reader/CSV.php | 49 +++++++++++++++++-- .../Examples/Reader/exampleReader14.php | 8 +-- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/Classes/PHPExcel/Reader/CSV.php b/Classes/PHPExcel/Reader/CSV.php index adc8a68b..b4472bc1 100644 --- a/Classes/PHPExcel/Reader/CSV.php +++ b/Classes/PHPExcel/Reader/CSV.php @@ -90,6 +90,14 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader */ private $_sheetIndex; + /** + * Load rows contiguously + * + * @access private + * @var int + */ + private $_contiguous; + /** * PHPExcel_Reader_IReadFilter instance * @@ -108,6 +116,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader $this->_lineEnding = PHP_EOL; $this->_sheetIndex = 0; $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter(); + $this->_contiguous = false; } // function __construct() /** @@ -228,10 +237,12 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader } // Loop through file - $currentRow = 0; + $currentRow = $contiguousRow = 0; $rowData = array(); while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) { ++$currentRow; + $crset = false; + $ccRef = 0; $rowDataCount = count($rowData); for ($i = 0; $i < $rowDataCount; ++$i) { $columnLetter = PHPExcel_Cell::stringFromColumnIndex($i); @@ -245,8 +256,17 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader $rowData[$i] = PHPExcel_Shared_String::ConvertEncoding($rowData[$i], 'UTF-8', $this->_inputEncoding); } - // Set cell value - $objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowData[$i]); + if ($this->_contiguous) { + if (!$crset) { + ++$contiguousRow; + $crset = true; + } + // Set cell value + $objPHPExcel->getActiveSheet()->getCell(PHPExcel_Cell::stringFromColumnIndex($ccRef++) . $contiguousRow)->setValue($rowData[$i]); + } else { + // Set cell value + $objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowData[$i]); + } } } } @@ -348,4 +368,27 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader $this->_sheetIndex = $pValue; return $this; } // function setSheetIndex() + + /** + * Set Contiguous + * + * @access public + * @param string $pValue Input encoding + */ + public function setContiguous($contiguous = false) + { + $this->_contiguous = $contiguous; + return $this; + } // function setInputEncoding() + + /** + * Get Contiguous + * + * @access public + * @return boolean + */ + public function getContiguous() { + return $this->_contiguous; + } // function getSheetIndex() + } diff --git a/Documentation/Examples/Reader/exampleReader14.php b/Documentation/Examples/Reader/exampleReader14.php index 3b19b541..b8a2b6d1 100644 --- a/Documentation/Examples/Reader/exampleReader14.php +++ b/Documentation/Examples/Reader/exampleReader14.php @@ -5,12 +5,12 @@ -PHPExcel Reader Example #14 +PHPExcel Reader Example #15 -

PHPExcel Reader Example #14

+

PHPExcel Reader Example #15

Reading a Large CSV file in "Chunks" to split across multiple Worksheets

setReadFilter($chunkFilter); +/** and that we want to store it in contiguous rows/columns **/ +$objReader->setReadFilter($chunkFilter) + ->setContiguous(true); /** Instantiate a new PHPExcel object manually **/