Performance improvements to the CSV Reader and Writer

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@64769 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2010-12-03 17:48:10 +00:00
parent 5917b8c9b0
commit f1e413bcc1
4 changed files with 21 additions and 20 deletions

View File

@ -31,6 +31,7 @@ PHPExcel_Shared_ZipStreamWrapper::register();
if (ini_get('mbstring.func_overload') & 2) { if (ini_get('mbstring.func_overload') & 2) {
throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).'); throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
} }
PHPExcel_Shared_String::buildCharacterSets();
class PHPExcel_Autoloader class PHPExcel_Autoloader

View File

@ -230,20 +230,22 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
break; break;
} }
$escapeEnclosures = array( "\\" . $this->_enclosure,
$this->_enclosure . $this->_enclosure
);
// Loop through file // Loop through file
$currentRow = $contiguousRow = 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; $crset = false;
$ccRef = 0;
$rowDataCount = count($rowData); $rowDataCount = count($rowData);
$columnLetter = 'A';
for ($i = 0; $i < $rowDataCount; ++$i) { for ($i = 0; $i < $rowDataCount; ++$i) {
$columnLetter = PHPExcel_Cell::stringFromColumnIndex($i);
if ($rowData[$i] != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) { if ($rowData[$i] != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
// Unescape enclosures // Unescape enclosures
$rowData[$i] = str_replace("\\" . $this->_enclosure, $this->_enclosure, $rowData[$i]); $rowData[$i] = str_replace($escapeEnclosures, $this->_enclosure, $rowData[$i]);
$rowData[$i] = str_replace($this->_enclosure . $this->_enclosure, $this->_enclosure, $rowData[$i]);
// Convert encoding if necessary // Convert encoding if necessary
if ($this->_inputEncoding !== 'UTF-8') { if ($this->_inputEncoding !== 'UTF-8') {
@ -256,12 +258,13 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
$crset = true; $crset = true;
} }
// Set cell value // Set cell value
$objPHPExcel->getActiveSheet()->getCell(PHPExcel_Cell::stringFromColumnIndex($ccRef++) . $contiguousRow)->setValue($rowData[$i]); $objPHPExcel->getActiveSheet()->getCell($columnLetter . $contiguousRow)->setValue($rowData[$i]);
} else { } else {
// Set cell value // Set cell value
$objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowData[$i]); $objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowData[$i]);
} }
} }
++$columnLetter;
} }
} }

View File

@ -194,11 +194,11 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
// echo 'Initial Cast to Boolean<br />'; // echo 'Initial Cast to Boolean<br />';
$value = isset($c->v) ? (string) $c->v : null; $value = isset($c->v) ? (string) $c->v : null;
if ($value == '0') { if ($value == '0') {
$value = false; return false;
} elseif ($value == '1') { } elseif ($value == '1') {
$value = true; return true;
} else { } else {
$value = (bool)$c->v; return (bool)$c->v;
} }
return $value; return $value;
} // function _castToBool() } // function _castToBool()

View File

@ -322,6 +322,15 @@ class PHPExcel_Shared_String
return true; return true;
} }
public static function buildCharacterSets() {
if(empty(self::$_controlCharacters)) {
self::_buildControlCharacters();
}
if(empty(self::$_SYLKCharacters)) {
self::_buildSYLKCharacters();
}
}
/** /**
* Convert from OpenXML escaped control character to PHP control character * Convert from OpenXML escaped control character to PHP control character
* *
@ -337,10 +346,6 @@ class PHPExcel_Shared_String
* @return string * @return string
*/ */
public static function ControlCharacterOOXML2PHP($value = '') { public static function ControlCharacterOOXML2PHP($value = '') {
if(empty(self::$_controlCharacters)) {
self::_buildControlCharacters();
}
return str_replace( array_keys(self::$_controlCharacters), array_values(self::$_controlCharacters), $value ); return str_replace( array_keys(self::$_controlCharacters), array_values(self::$_controlCharacters), $value );
} }
@ -359,10 +364,6 @@ class PHPExcel_Shared_String
* @return string * @return string
*/ */
public static function ControlCharacterPHP2OOXML($value = '') { public static function ControlCharacterPHP2OOXML($value = '') {
if(empty(self::$_controlCharacters)) {
self::_buildControlCharacters();
}
return str_replace( array_values(self::$_controlCharacters), array_keys(self::$_controlCharacters), $value ); return str_replace( array_values(self::$_controlCharacters), array_keys(self::$_controlCharacters), $value );
} }
@ -657,10 +658,6 @@ class PHPExcel_Shared_String
return $pValue; return $pValue;
} }
if(empty(self::$_SYLKCharacters)) {
self::_buildSYLKCharacters();
}
foreach (self::$_SYLKCharacters as $k => $v) { foreach (self::$_SYLKCharacters as $k => $v) {
$pValue = str_replace($k, $v, $pValue); $pValue = str_replace($k, $v, $pValue);
} }