From f1e413bcc1f69f75e55b2a7e55452cb0bf5a11a1 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Fri, 3 Dec 2010 17:48:10 +0000 Subject: [PATCH] Performance improvements to the CSV Reader and Writer git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@64769 2327b42d-5241-43d6-9e2a-de5ac946f064 --- Classes/PHPExcel/Autoloader.php | 1 + Classes/PHPExcel/Reader/CSV.php | 13 ++++++++----- Classes/PHPExcel/Reader/Excel2007.php | 6 +++--- Classes/PHPExcel/Shared/String.php | 21 +++++++++------------ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Classes/PHPExcel/Autoloader.php b/Classes/PHPExcel/Autoloader.php index 3a51ea4..51336c2 100644 --- a/Classes/PHPExcel/Autoloader.php +++ b/Classes/PHPExcel/Autoloader.php @@ -31,6 +31,7 @@ PHPExcel_Shared_ZipStreamWrapper::register(); if (ini_get('mbstring.func_overload') & 2) { throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).'); } +PHPExcel_Shared_String::buildCharacterSets(); class PHPExcel_Autoloader diff --git a/Classes/PHPExcel/Reader/CSV.php b/Classes/PHPExcel/Reader/CSV.php index 6c79462..717a685 100644 --- a/Classes/PHPExcel/Reader/CSV.php +++ b/Classes/PHPExcel/Reader/CSV.php @@ -230,20 +230,22 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader break; } + $escapeEnclosures = array( "\\" . $this->_enclosure, + $this->_enclosure . $this->_enclosure + ); + // Loop through file $currentRow = $contiguousRow = 0; $rowData = array(); while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) { ++$currentRow; $crset = false; - $ccRef = 0; $rowDataCount = count($rowData); + $columnLetter = 'A'; 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]); + $rowData[$i] = str_replace($escapeEnclosures, $this->_enclosure, $rowData[$i]); // Convert encoding if necessary if ($this->_inputEncoding !== 'UTF-8') { @@ -256,12 +258,13 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader $crset = true; } // Set cell value - $objPHPExcel->getActiveSheet()->getCell(PHPExcel_Cell::stringFromColumnIndex($ccRef++) . $contiguousRow)->setValue($rowData[$i]); + $objPHPExcel->getActiveSheet()->getCell($columnLetter . $contiguousRow)->setValue($rowData[$i]); } else { // Set cell value $objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowData[$i]); } } + ++$columnLetter; } } diff --git a/Classes/PHPExcel/Reader/Excel2007.php b/Classes/PHPExcel/Reader/Excel2007.php index 315a34f..7a5c2fd 100644 --- a/Classes/PHPExcel/Reader/Excel2007.php +++ b/Classes/PHPExcel/Reader/Excel2007.php @@ -194,11 +194,11 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader // echo 'Initial Cast to Boolean
'; $value = isset($c->v) ? (string) $c->v : null; if ($value == '0') { - $value = false; + return false; } elseif ($value == '1') { - $value = true; + return true; } else { - $value = (bool)$c->v; + return (bool)$c->v; } return $value; } // function _castToBool() diff --git a/Classes/PHPExcel/Shared/String.php b/Classes/PHPExcel/Shared/String.php index 784c313..dabb245 100644 --- a/Classes/PHPExcel/Shared/String.php +++ b/Classes/PHPExcel/Shared/String.php @@ -322,6 +322,15 @@ class PHPExcel_Shared_String 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 * @@ -337,10 +346,6 @@ class PHPExcel_Shared_String * @return string */ public static function ControlCharacterOOXML2PHP($value = '') { - if(empty(self::$_controlCharacters)) { - self::_buildControlCharacters(); - } - return str_replace( array_keys(self::$_controlCharacters), array_values(self::$_controlCharacters), $value ); } @@ -359,10 +364,6 @@ class PHPExcel_Shared_String * @return string */ public static function ControlCharacterPHP2OOXML($value = '') { - if(empty(self::$_controlCharacters)) { - self::_buildControlCharacters(); - } - return str_replace( array_values(self::$_controlCharacters), array_keys(self::$_controlCharacters), $value ); } @@ -657,10 +658,6 @@ class PHPExcel_Shared_String return $pValue; } - if(empty(self::$_SYLKCharacters)) { - self::_buildSYLKCharacters(); - } - foreach (self::$_SYLKCharacters as $k => $v) { $pValue = str_replace($k, $v, $pValue); }