Refactoring of canRead function in Readers, and minor fixes to Examples and documentation updates

This commit is contained in:
Mark Baker 2013-05-06 23:39:49 +01:00
parent 216ef82a14
commit fd2a3773d8
9 changed files with 299 additions and 219 deletions

View File

@ -67,6 +67,8 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
*/ */
protected $_readFilter = NULL; protected $_readFilter = NULL;
protected $_fileHandle = NULL;
/** /**
* Read data only? * Read data only?
@ -79,7 +81,6 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
return $this->_readDataOnly; return $this->_readDataOnly;
} }
/** /**
* Set read data only * Set read data only
* Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information. * Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
@ -94,7 +95,6 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/** /**
* Read charts in workbook? * Read charts in workbook?
* If this is true, then the Reader will include any charts that exist in the workbook. * If this is true, then the Reader will include any charts that exist in the workbook.
@ -107,7 +107,6 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
return $this->_includeCharts; return $this->_includeCharts;
} }
/** /**
* Set read charts in workbook * Set read charts in workbook
* Set to true, to advise the Reader to include any charts that exist in the workbook. * Set to true, to advise the Reader to include any charts that exist in the workbook.
@ -123,7 +122,6 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/** /**
* Get which sheets to load * Get which sheets to load
* Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null * Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
@ -136,7 +134,6 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
return $this->_loadSheetsOnly; return $this->_loadSheetsOnly;
} }
/** /**
* Set which sheets to load * Set which sheets to load
* *
@ -153,7 +150,6 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/** /**
* Set all sheets to load * Set all sheets to load
* Tells the Reader to load all worksheets from the workbook. * Tells the Reader to load all worksheets from the workbook.
@ -166,7 +162,6 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/** /**
* Read filter * Read filter
* *
@ -176,7 +171,6 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
return $this->_readFilter; return $this->_readFilter;
} }
/** /**
* Set read filter * Set read filter
* *
@ -188,5 +182,46 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/**
* Open file for reading
*
* @param string $pFilename
* @throws PHPExcel_Reader_Exception
* @return resource
*/
protected function _openFile($pFilename)
{
// Check if file exists
if (!file_exists($pFilename) || !is_readable($pFilename)) {
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
// Open file
$this->_fileHandle = fopen($pFilename, 'r');
if ($this->_fileHandle === FALSE) {
throw new PHPExcel_Reader_Exception("Could not open file " . $pFilename . " for reading.");
}
}
/**
* Can the current PHPExcel_Reader_IReader read the file?
*
* @param string $pFilename
* @return boolean
* @throws PHPExcel_Reader_Exception
*/
public function canRead($pFilename)
{
// Check if file exists
try {
$this->_openFile($pFilename);
} catch (Exception $e) {
return FALSE;
}
$readable = $this->_isValidFormat();
fclose ($this->_fileHandle);
return $readable;
}
} }

View File

@ -92,112 +92,104 @@ class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_R
*/ */
private $_contiguous = false; private $_contiguous = false;
/** /**
* Row counter for loading rows contiguously * Row counter for loading rows contiguously
* *
* @access private
* @var int * @var int
*/ */
private $_contiguousRow = -1; private $_contiguousRow = -1;
/** /**
* Create a new PHPExcel_Reader_CSV * Create a new PHPExcel_Reader_CSV
*/ */
public function __construct() { public function __construct() {
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter(); $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
} // function __construct() }
/** /**
* Can the current PHPExcel_Reader_IReader read the file? * Validate that the current file is a CSV file
* *
* @access public
* @param string $pFilename
* @return boolean * @return boolean
* @throws PHPExcel_Reader_Exception
*/ */
public function canRead($pFilename) protected function _isValidFormat()
{ {
// Check if file exists return TRUE;
if (!file_exists($pFilename)) { }
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
return true;
} // function canRead()
/** /**
* Set input encoding * Set input encoding
* *
* @access public
* @param string $pValue Input encoding * @param string $pValue Input encoding
*/ */
public function setInputEncoding($pValue = 'UTF-8') public function setInputEncoding($pValue = 'UTF-8')
{ {
$this->_inputEncoding = $pValue; $this->_inputEncoding = $pValue;
return $this; return $this;
} // function setInputEncoding() }
/** /**
* Get input encoding * Get input encoding
* *
* @access public
* @return string * @return string
*/ */
public function getInputEncoding() public function getInputEncoding()
{ {
return $this->_inputEncoding; return $this->_inputEncoding;
} // function getInputEncoding() }
/**
* Move filepointer past any BOM marker
*
*/
protected function _skipBOM()
{
rewind($fileHandle);
switch ($this->_inputEncoding) {
case 'UTF-8':
fgets($this->_fileHandle, 4) == "\xEF\xBB\xBF" ?
fseek($this->_fileHandle, 3) : fseek($this->_fileHandle, 0);
break;
case 'UTF-16LE':
fgets($this->_fileHandle, 3) == "\xFF\xFE" ?
fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
break;
case 'UTF-16BE':
fgets($this->_fileHandle, 3) == "\xFE\xFF" ?
fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
break;
case 'UTF-32LE':
fgets($this->_fileHandle, 5) == "\xFF\xFE\x00\x00" ?
fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
break;
case 'UTF-32BE':
fgets($this->_fileHandle, 5) == "\x00\x00\xFE\xFF" ?
fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
break;
default:
break;
}
}
/** /**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns) * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
* *
* @access public
* @param string $pFilename * @param string $pFilename
* @throws PHPExcel_Reader_Exception * @throws PHPExcel_Reader_Exception
*/ */
public function listWorksheetInfo($pFilename) public function listWorksheetInfo($pFilename)
{ {
// Check if file exists
if (!file_exists($pFilename)) {
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
// Open file // Open file
$fileHandle = fopen($pFilename, 'r'); $this->_openFile($pFilename);
if ($fileHandle === false) { if (!$this->_isValidFormat()) {
throw new PHPExcel_Reader_Exception("Could not open file " . $pFilename . " for reading."); fclose ($this->_fileHandle);
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
} }
$fileHandle = $this->_fileHandle;
// Skip BOM, if any // Skip BOM, if any
switch ($this->_inputEncoding) { $this->_skipBOM();
case 'UTF-8':
fgets($fileHandle, 4) == "\xEF\xBB\xBF" ?
fseek($fileHandle, 3) : fseek($fileHandle, 0);
break;
case 'UTF-16LE':
fgets($fileHandle, 3) == "\xFF\xFE" ?
fseek($fileHandle, 2) : fseek($fileHandle, 0);
break;
case 'UTF-16BE':
fgets($fileHandle, 3) == "\xFE\xFF" ?
fseek($fileHandle, 2) : fseek($fileHandle, 0);
break;
case 'UTF-32LE':
fgets($fileHandle, 5) == "\xFF\xFE\x00\x00" ?
fseek($fileHandle, 4) : fseek($fileHandle, 0);
break;
case 'UTF-32BE':
fgets($fileHandle, 5) == "\x00\x00\xFE\xFF" ?
fseek($fileHandle, 4) : fseek($fileHandle, 0);
break;
default:
break;
}
$escapeEnclosures = array( "\\" . $this->_enclosure, $this->_enclosure . $this->_enclosure ); $escapeEnclosures = array( "\\" . $this->_enclosure, $this->_enclosure . $this->_enclosure );
@ -223,11 +215,9 @@ class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_R
return $worksheetInfo; return $worksheetInfo;
} }
/** /**
* Loads PHPExcel from file * Loads PHPExcel from file
* *
* @access public
* @param string $pFilename * @param string $pFilename
* @return PHPExcel * @return PHPExcel
* @throws PHPExcel_Reader_Exception * @throws PHPExcel_Reader_Exception
@ -239,13 +229,11 @@ class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_R
// Load into this instance // Load into this instance
return $this->loadIntoExisting($pFilename, $objPHPExcel); return $this->loadIntoExisting($pFilename, $objPHPExcel);
} // function load() }
/** /**
* Loads PHPExcel from file into PHPExcel instance * Loads PHPExcel from file into PHPExcel instance
* *
* @access public
* @param string $pFilename * @param string $pFilename
* @param PHPExcel $objPHPExcel * @param PHPExcel $objPHPExcel
* @return PHPExcel * @return PHPExcel
@ -253,51 +241,25 @@ class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_R
*/ */
public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel) public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
{ {
// Check if file exists
if (!file_exists($pFilename)) {
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
// Create new PHPExcel
while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
$objPHPExcel->createSheet();
}
$sheet = $objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
$lineEnding = ini_get('auto_detect_line_endings'); $lineEnding = ini_get('auto_detect_line_endings');
ini_set('auto_detect_line_endings', true); ini_set('auto_detect_line_endings', true);
// Open file // Open file
$fileHandle = fopen($pFilename, 'r'); $this->_openFile($pFilename);
if ($fileHandle === false) { if (!$this->_isValidFormat()) {
throw new PHPExcel_Reader_Exception("Could not open file $pFilename for reading."); fclose ($this->_fileHandle);
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
} }
$fileHandle = $this->_fileHandle;
// Skip BOM, if any // Skip BOM, if any
switch ($this->_inputEncoding) { $this->_skipBOM();
case 'UTF-8':
fgets($fileHandle, 4) == "\xEF\xBB\xBF" ? // Create new PHPExcel object
fseek($fileHandle, 3) : fseek($fileHandle, 0); while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
break; $objPHPExcel->createSheet();
case 'UTF-16LE':
fgets($fileHandle, 3) == "\xFF\xFE" ?
fseek($fileHandle, 2) : fseek($fileHandle, 0);
break;
case 'UTF-16BE':
fgets($fileHandle, 3) == "\xFE\xFF" ?
fseek($fileHandle, 2) : fseek($fileHandle, 0);
break;
case 'UTF-32LE':
fgets($fileHandle, 5) == "\xFF\xFE\x00\x00" ?
fseek($fileHandle, 4) : fseek($fileHandle, 0);
break;
case 'UTF-32BE':
fgets($fileHandle, 5) == "\x00\x00\xFE\xFF" ?
fseek($fileHandle, 4) : fseek($fileHandle, 0);
break;
default:
break;
} }
$sheet = $objPHPExcel->setActiveSheetIndex($this->_sheetIndex);
$escapeEnclosures = array( "\\" . $this->_enclosure, $escapeEnclosures = array( "\\" . $this->_enclosure,
$this->_enclosure . $this->_enclosure $this->_enclosure . $this->_enclosure
@ -341,48 +303,40 @@ class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_R
// Return // Return
return $objPHPExcel; return $objPHPExcel;
} // function loadIntoExisting() }
/** /**
* Get delimiter * Get delimiter
* *
* @access public
* @return string * @return string
*/ */
public function getDelimiter() { public function getDelimiter() {
return $this->_delimiter; return $this->_delimiter;
} // function getDelimiter() }
/** /**
* Set delimiter * Set delimiter
* *
* @access public
* @param string $pValue Delimiter, defaults to , * @param string $pValue Delimiter, defaults to ,
* @return PHPExcel_Reader_CSV * @return PHPExcel_Reader_CSV
*/ */
public function setDelimiter($pValue = ',') { public function setDelimiter($pValue = ',') {
$this->_delimiter = $pValue; $this->_delimiter = $pValue;
return $this; return $this;
} // function setDelimiter() }
/** /**
* Get enclosure * Get enclosure
* *
* @access public
* @return string * @return string
*/ */
public function getEnclosure() { public function getEnclosure() {
return $this->_enclosure; return $this->_enclosure;
} // function getEnclosure() }
/** /**
* Set enclosure * Set enclosure
* *
* @access public
* @param string $pValue Enclosure, defaults to " * @param string $pValue Enclosure, defaults to "
* @return PHPExcel_Reader_CSV * @return PHPExcel_Reader_CSV
*/ */
@ -392,82 +346,70 @@ class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_R
} }
$this->_enclosure = $pValue; $this->_enclosure = $pValue;
return $this; return $this;
} // function setEnclosure() }
/** /**
* Get line ending * Get line ending
* *
* @access public
* @return string * @return string
*/ */
public function getLineEnding() { public function getLineEnding() {
return $this->_lineEnding; return $this->_lineEnding;
} // function getLineEnding() }
/** /**
* Set line ending * Set line ending
* *
* @access public
* @param string $pValue Line ending, defaults to OS line ending (PHP_EOL) * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
* @return PHPExcel_Reader_CSV * @return PHPExcel_Reader_CSV
*/ */
public function setLineEnding($pValue = PHP_EOL) { public function setLineEnding($pValue = PHP_EOL) {
$this->_lineEnding = $pValue; $this->_lineEnding = $pValue;
return $this; return $this;
} // function setLineEnding() }
/** /**
* Get sheet index * Get sheet index
* *
* @access public * @return integer
* @return int
*/ */
public function getSheetIndex() { public function getSheetIndex() {
return $this->_sheetIndex; return $this->_sheetIndex;
} // function getSheetIndex() }
/** /**
* Set sheet index * Set sheet index
* *
* @access public
* @param integer $pValue Sheet index * @param integer $pValue Sheet index
* @return PHPExcel_Reader_CSV * @return PHPExcel_Reader_CSV
*/ */
public function setSheetIndex($pValue = 0) { public function setSheetIndex($pValue = 0) {
$this->_sheetIndex = $pValue; $this->_sheetIndex = $pValue;
return $this; return $this;
} // function setSheetIndex() }
/** /**
* Set Contiguous * Set Contiguous
* *
* @access public
* @param boolean $contiguous * @param boolean $contiguous
*/ */
public function setContiguous($contiguous = FALSE) public function setContiguous($contiguous = FALSE)
{ {
$this->_contiguous = (bool) $contiguous; $this->_contiguous = (bool) $contiguous;
if (!$contiguous) { if (!$contiguous) {
$this->_contiguousRow = -1; $this->_contiguousRow = -1;
} }
return $this; return $this;
} // function setInputEncoding() }
/** /**
* Get Contiguous * Get Contiguous
* *
* @access public
* @return boolean * @return boolean
*/ */
public function getContiguous() { public function getContiguous() {
return $this->_contiguous; return $this->_contiguous;
} // function getSheetIndex() }
} }

View File

@ -92,15 +92,13 @@ class PHPExcel_Reader_Excel2003XML extends PHPExcel_Reader_Abstract implements P
'<?mso-application progid="Excel.Sheet"?>' '<?mso-application progid="Excel.Sheet"?>'
); );
// Check if file exists // Open file
if (!file_exists($pFilename)) { $this->_openFile($pFilename);
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist."); $fileHandle = $this->_fileHandle;
}
// Read sample data (first 2 KB will do) // Read sample data (first 2 KB will do)
$fh = fopen($pFilename, 'r'); $data = fread($fileHandle, 2048);
$data = fread($fh, 2048); fclose($fileHandle);
fclose($fh);
$valid = true; $valid = true;
foreach($signature as $match) { foreach($signature as $match) {

View File

@ -109,24 +109,14 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
} }
/** /**
* Can the current PHPExcel_Reader_IReader read the file? * Validate that the current file is an HTML file
* *
* @param string $pFilename * @return boolean
* @return boolean
* @throws PHPExcel_Reader_Exception
*/ */
public function canRead($pFilename) protected function _isValidFormat()
{ {
// Check if file exists // Reading 2048 bytes should be enough to validate that the format is HTML
if (!file_exists($pFilename)) { $data = fread($this->_fileHandle, 2048);
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
// Read sample data (first 2 KB will do)
$fh = fopen($pFilename, 'r');
$data = fread($fh, 2048);
fclose($fh);
if ((strpos('<',$data) !== FALSE) && if ((strpos('<',$data) !== FALSE) &&
(strlen($data) !== strlen(strip_tags($data)))) { (strlen($data) !== strlen(strip_tags($data)))) {
return TRUE; return TRUE;
@ -416,14 +406,14 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
*/ */
public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel) public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
{ {
// Check if file exists // Open file to validate
if (!file_exists($pFilename)) { $this->_openFile($pFilename);
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist."); if (!$this->_isValidFormat()) {
} fclose ($this->_fileHandle);
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid HTML file.");
if (!is_file($pFilename)) {
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! The given file is not a regular file.");
} }
// Close after validating
fclose ($this->_fileHandle);
// Create new PHPExcel // Create new PHPExcel
while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) { while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
@ -433,9 +423,9 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
// Create a new DOM object // Create a new DOM object
$dom = new domDocument; $dom = new domDocument;
// Load the HTML file into the DOM object // Reload the HTML file into the DOM object
$loaded = $dom->loadHTMLFile($pFilename); $loaded = $dom->loadHTMLFile($pFilename);
if ($loaded === false) { if ($loaded === FALSE) {
throw new PHPExcel_Reader_Exception('Failed to load ',$pFilename,' as a DOM Document'); throw new PHPExcel_Reader_Exception('Failed to load ',$pFilename,' as a DOM Document');
} }

View File

@ -79,42 +79,31 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter(); $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
} }
/** /**
* Can the current PHPExcel_Reader_IReader read the file? * Validate that the current file is a SYLK file
* *
* @param string $pFilename * @return boolean
* @return boolean
* @throws PHPExcel_Reader_Exception
*/ */
public function canRead($pFilename) protected function _isValidFormat()
{ {
// Check if file exists
if (!file_exists($pFilename)) {
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
// Read sample data (first 2 KB will do) // Read sample data (first 2 KB will do)
$fh = fopen($pFilename, 'r'); $data = fread($this->_fileHandle, 2048);
$data = fread($fh, 2048);
fclose($fh);
// Count delimiters in file // Count delimiters in file
$delimiterCount = substr_count($data, ';'); $delimiterCount = substr_count($data, ';');
if ($delimiterCount < 1) { if ($delimiterCount < 1) {
return false; return FALSE;
} }
// Analyze first line looking for ID; signature // Analyze first line looking for ID; signature
$lines = explode("\n", $data); $lines = explode("\n", $data);
if (substr($lines[0],0,4) != 'ID;P') { if (substr($lines[0],0,4) != 'ID;P') {
return false; return FALSE;
} }
return true; return TRUE;
} }
/** /**
* Set input encoding * Set input encoding
* *
@ -126,7 +115,6 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
return $this; return $this;
} }
/** /**
* Get input encoding * Get input encoding
* *
@ -137,7 +125,6 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
return $this->_inputEncoding; return $this->_inputEncoding;
} }
/** /**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns) * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
* *
@ -146,16 +133,13 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
*/ */
public function listWorksheetInfo($pFilename) public function listWorksheetInfo($pFilename)
{ {
// Check if file exists
if (!file_exists($pFilename)) {
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
// Open file // Open file
$fileHandle = fopen($pFilename, 'r'); $this->_openFile($pFilename);
if ($fileHandle === false) { if (!$this->_isValidFormat()) {
throw new PHPExcel_Reader_Exception("Could not open file " . $pFilename . " for reading."); fclose ($this->_fileHandle);
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
} }
$fileHandle = $this->_fileHandle;
$worksheetInfo = array(); $worksheetInfo = array();
$worksheetInfo[0]['worksheetName'] = 'Worksheet'; $worksheetInfo[0]['worksheetName'] = 'Worksheet';
@ -177,7 +161,7 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
// explode each row at semicolons while taking into account that literal semicolon (;) // explode each row at semicolons while taking into account that literal semicolon (;)
// is escaped like this (;;) // is escaped like this (;;)
$rowData = explode("\t",str_replace('?',';',str_replace(';',"\t",str_replace(';;','?',rtrim($rowData))))); $rowData = explode("\t",str_replace('¤',';',str_replace(';',"\t",str_replace(';;','¤',rtrim($rowData)))));
$dataType = array_shift($rowData); $dataType = array_shift($rowData);
if ($dataType == 'C') { if ($dataType == 'C') {
@ -209,7 +193,6 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
return $worksheetInfo; return $worksheetInfo;
} }
/** /**
* Loads PHPExcel from file * Loads PHPExcel from file
* *
@ -226,7 +209,6 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
return $this->loadIntoExisting($pFilename, $objPHPExcel); return $this->loadIntoExisting($pFilename, $objPHPExcel);
} }
/** /**
* Loads PHPExcel from file into PHPExcel instance * Loads PHPExcel from file into PHPExcel instance
* *
@ -237,10 +219,14 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
*/ */
public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel) public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
{ {
// Check if file exists // Open file
if (!file_exists($pFilename)) { $this->_openFile($pFilename);
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist."); if (!$this->_isValidFormat()) {
fclose ($this->_fileHandle);
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
} }
$fileHandle = $this->_fileHandle;
rewind($fileHandle);
// Create new PHPExcel // Create new PHPExcel
while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) { while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
@ -251,12 +237,6 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
$fromFormats = array('\-', '\ '); $fromFormats = array('\-', '\ ');
$toFormats = array('-', ' '); $toFormats = array('-', ' ');
// Open file
$fileHandle = fopen($pFilename, 'r');
if ($fileHandle === false) {
throw new PHPExcel_Reader_Exception("Could not open file $pFilename for reading.");
}
// Loop through file // Loop through file
$rowData = array(); $rowData = array();
$column = $row = ''; $column = $row = '';
@ -404,7 +384,9 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
} }
if (($formatStyle > '') && ($column > '') && ($row > '')) { if (($formatStyle > '') && ($column > '') && ($row > '')) {
$columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1); $columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1);
$objPHPExcel->getActiveSheet()->getStyle($columnLetter.$row)->applyFromArray($this->_formats[$formatStyle]); if (isset($this->_formats[$formatStyle])) {
$objPHPExcel->getActiveSheet()->getStyle($columnLetter.$row)->applyFromArray($this->_formats[$formatStyle]);
}
} }
if ((!empty($styleData)) && ($column > '') && ($row > '')) { if ((!empty($styleData)) && ($column > '') && ($row > '')) {
$columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1); $columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1);
@ -444,7 +426,6 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
return $objPHPExcel; return $objPHPExcel;
} }
/** /**
* Get sheet index * Get sheet index
* *
@ -454,7 +435,6 @@ class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_
return $this->_sheetIndex; return $this->_sheetIndex;
} }
/** /**
* Set sheet index * Set sheet index
* *

View File

@ -0,0 +1,138 @@
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
date_default_timezone_set('Europe/London');
/**
* PHPExcel
*
* Copyright (C) 2006 - 2012 PHPExcel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPExcel
* @package PHPExcel
* @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE##
*/
/** PHPExcel */
require_once '../Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
array(
array('', 2010, 2011, 2012),
array('Q1', 12, 15, 21),
array('Q2', 56, 73, 86),
array('Q3', 52, 61, 69),
array('Q4', 30, 32, 0),
)
);
// Set the Labels for each data series we want to plot
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$dataseriesLabels = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1), // 2010
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), // 2011
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1), // 2012
);
// Set the X-Axis Labels
$xAxisTickValues = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4), // Q1 to Q4
);
// Set the Data values for each data series we want to plot
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$dataSeriesValues = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', NULL, 4),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', NULL, 4),
);
// Build the dataseries
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_SCATTERCHART, // plotType
NULL, // plotGrouping (Scatter charts don't have any grouping)
range(0, count($dataSeriesValues)-1), // plotOrder
$dataseriesLabels, // plotLabel
$xAxisTickValues, // plotCategory
$dataSeriesValues, // plotValues
NULL, // smooth line
PHPExcel_Chart_DataSeries::STYLE_LINEMARKER // plotStyle
);
// Set the series in the plot area
$plotarea = new PHPExcel_Chart_PlotArea(NULL, array($series));
// Set the chart legend
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_TOPRIGHT, NULL, false);
$title = new PHPExcel_Chart_Title('Test Scatter Chart');
$yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');
// Create the chart
$chart = new PHPExcel_Chart(
'chart1', // name
$title, // title
$legend, // legend
$plotarea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
$yAxisLabel // yAxisLabel
);
// Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('A7');
$chart->setBottomRightPosition('H20');
// Add the chart to the worksheet
$objWorksheet->addChart($chart);
// Save Excel 2007 file
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
// Echo memory peak usage
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
// Echo done
echo date('H:i:s') , " Done writing file" , EOL;
echo 'File has been created in ' , getcwd() , EOL;

View File

@ -36,11 +36,8 @@ date_default_timezone_set('Europe/London');
* @version ##VERSION##, ##DATE## * @version ##VERSION##, ##DATE##
*/ */
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
/** PHPExcel */ /** PHPExcel */
include 'PHPExcel.php'; include '../Classes/PHPExcel.php';
if (!file_exists("33chartcreate-bar.xlsx")) { if (!file_exists("33chartcreate-bar.xlsx")) {
exit("Please run 33chartcreate-bar.php first." . EOL); exit("Please run 33chartcreate-bar.php first." . EOL);