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
This commit is contained in:
Mark Baker 2010-10-23 22:25:48 +00:00
parent 22f704e4b1
commit c448f2ed7a
2 changed files with 51 additions and 6 deletions

View File

@ -90,6 +90,14 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
*/ */
private $_sheetIndex; private $_sheetIndex;
/**
* Load rows contiguously
*
* @access private
* @var int
*/
private $_contiguous;
/** /**
* PHPExcel_Reader_IReadFilter instance * PHPExcel_Reader_IReadFilter instance
* *
@ -108,6 +116,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
$this->_lineEnding = PHP_EOL; $this->_lineEnding = PHP_EOL;
$this->_sheetIndex = 0; $this->_sheetIndex = 0;
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter(); $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
$this->_contiguous = false;
} // function __construct() } // function __construct()
/** /**
@ -228,10 +237,12 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
} }
// Loop through file // Loop through file
$currentRow = 0; $currentRow = $contiguousRow = 0;
$rowData = array(); $rowData = array();
while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) { while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
++$currentRow; ++$currentRow;
$crset = false;
$ccRef = 0;
$rowDataCount = count($rowData); $rowDataCount = count($rowData);
for ($i = 0; $i < $rowDataCount; ++$i) { for ($i = 0; $i < $rowDataCount; ++$i) {
$columnLetter = PHPExcel_Cell::stringFromColumnIndex($i); $columnLetter = PHPExcel_Cell::stringFromColumnIndex($i);
@ -245,11 +256,20 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
$rowData[$i] = PHPExcel_Shared_String::ConvertEncoding($rowData[$i], 'UTF-8', $this->_inputEncoding); $rowData[$i] = PHPExcel_Shared_String::ConvertEncoding($rowData[$i], 'UTF-8', $this->_inputEncoding);
} }
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 // Set cell value
$objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowData[$i]); $objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowData[$i]);
} }
} }
} }
}
// Close file // Close file
fclose($fileHandle); fclose($fileHandle);
@ -348,4 +368,27 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
$this->_sheetIndex = $pValue; $this->_sheetIndex = $pValue;
return $this; return $this;
} // function setSheetIndex() } // 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()
} }

View File

@ -5,12 +5,12 @@
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHPExcel Reader Example #14</title> <title>PHPExcel Reader Example #15</title>
</head> </head>
<body> <body>
<h1>PHPExcel Reader Example #14</h1> <h1>PHPExcel Reader Example #15</h1>
<h2>Reading a Large CSV file in "Chunks" to split across multiple Worksheets</h2> <h2>Reading a Large CSV file in "Chunks" to split across multiple Worksheets</h2>
<?php <?php
@ -61,7 +61,9 @@ $chunkSize = 100;
$chunkFilter = new chunkReadFilter(); $chunkFilter = new chunkReadFilter();
/** Tell the Reader that we want to use the Read Filter that we've Instantiated **/ /** Tell the Reader that we want to use the Read Filter that we've Instantiated **/
$objReader->setReadFilter($chunkFilter); /** and that we want to store it in contiguous rows/columns **/
$objReader->setReadFilter($chunkFilter)
->setContiguous(true);
/** Instantiate a new PHPExcel object manually **/ /** Instantiate a new PHPExcel object manually **/