New Reader Feature: listWorksheetInfo() method added to Readers... courtesy of Christopher Mullins (schir1964)

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@88720 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2012-03-31 19:17:24 +00:00
parent 47c94fb28a
commit 63c411791e
7 changed files with 953 additions and 222 deletions

View File

@ -109,6 +109,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
*/ */
private $_readFilter = null; private $_readFilter = null;
/** /**
* Create a new PHPExcel_Reader_CSV * Create a new PHPExcel_Reader_CSV
*/ */
@ -116,6 +117,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter(); $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
} // function __construct() } // function __construct()
/** /**
* Can the current PHPExcel_Reader_IReader read the file? * Can the current PHPExcel_Reader_IReader read the file?
* *
@ -134,6 +136,126 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return true; return true;
} // function canRead() } // function canRead()
/**
* Read filter
*
* @access public
* @return PHPExcel_Reader_IReadFilter
*/
public function getReadFilter() {
return $this->_readFilter;
} // function getReadFilter()
/**
* Set read filter
*
* @access public
* @param PHPExcel_Reader_IReadFilter $pValue
*/
public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
$this->_readFilter = $pValue;
return $this;
} // function setReadFilter()
/**
* Set input encoding
*
* @access public
* @param string $pValue Input encoding
*/
public function setInputEncoding($pValue = 'UTF-8')
{
$this->_inputEncoding = $pValue;
return $this;
} // function setInputEncoding()
/**
* Get input encoding
*
* @access public
* @return string
*/
public function getInputEncoding()
{
return $this->_inputEncoding;
} // function getInputEncoding()
/**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
*
* @access public
* @param string $pFilename
* @throws Exception
*/
public function listWorksheetInfo($pFilename)
{
// Check if file exists
if (!file_exists($pFilename)) {
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
// Open file
$fileHandle = fopen($pFilename, 'r');
if ($fileHandle === false) {
throw new Exception("Could not open file " . $pFilename . " for reading.");
}
// Skip BOM, if any
switch ($this->_inputEncoding) {
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 );
$worksheetInfo = array();
$worksheetInfo[0]['worksheetName'] = 'Worksheet';
$worksheetInfo[0]['lastColumnLetter'] = 'A';
$worksheetInfo[0]['lastColumnIndex'] = 0;
$worksheetInfo[0]['totalRows'] = 0;
$worksheetInfo[0]['totalColumns'] = 0;
// Loop through each line of the file in turn
while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
$worksheetInfo[0]['totalRows']++;
$worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
}
$worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
$worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
// Close file
fclose($fileHandle);
return $worksheetInfo;
}
/** /**
* Loads PHPExcel from file * Loads PHPExcel from file
* *
@ -151,49 +273,6 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return $this->loadIntoExisting($pFilename, $objPHPExcel); return $this->loadIntoExisting($pFilename, $objPHPExcel);
} // function load() } // function load()
/**
* Read filter
*
* @access public
* @return PHPExcel_Reader_IReadFilter
*/
public function getReadFilter() {
return $this->_readFilter;
} // function getReadFilter()
/**
* Set read filter
*
* @access public
* @param PHPExcel_Reader_IReadFilter $pValue
*/
public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
$this->_readFilter = $pValue;
return $this;
} // function setReadFilter()
/**
* Set input encoding
*
* @access public
* @param string $pValue Input encoding
*/
public function setInputEncoding($pValue = 'UTF-8')
{
$this->_inputEncoding = $pValue;
return $this;
} // function setInputEncoding()
/**
* Get input encoding
*
* @access public
* @return string
*/
public function getInputEncoding()
{
return $this->_inputEncoding;
} // function getInputEncoding()
/** /**
* Loads PHPExcel from file into PHPExcel instance * Loads PHPExcel from file into PHPExcel instance
@ -296,6 +375,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return $objPHPExcel; return $objPHPExcel;
} // function loadIntoExisting() } // function loadIntoExisting()
/** /**
* Get delimiter * Get delimiter
* *
@ -306,6 +386,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return $this->_delimiter; return $this->_delimiter;
} // function getDelimiter() } // function getDelimiter()
/** /**
* Set delimiter * Set delimiter
* *
@ -318,6 +399,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return $this; return $this;
} // function setDelimiter() } // function setDelimiter()
/** /**
* Get enclosure * Get enclosure
* *
@ -328,6 +410,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return $this->_enclosure; return $this->_enclosure;
} // function getEnclosure() } // function getEnclosure()
/** /**
* Set enclosure * Set enclosure
* *
@ -343,6 +426,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return $this; return $this;
} // function setEnclosure() } // function setEnclosure()
/** /**
* Get line ending * Get line ending
* *
@ -353,6 +437,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return $this->_lineEnding; return $this->_lineEnding;
} // function getLineEnding() } // function getLineEnding()
/** /**
* Set line ending * Set line ending
* *
@ -365,6 +450,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return $this; return $this;
} // function setLineEnding() } // function setLineEnding()
/** /**
* Get sheet index * Get sheet index
* *
@ -375,6 +461,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return $this->_sheetIndex; return $this->_sheetIndex;
} // function getSheetIndex() } // function getSheetIndex()
/** /**
* Set sheet index * Set sheet index
* *
@ -387,6 +474,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return $this; return $this;
} // function setSheetIndex() } // function setSheetIndex()
/** /**
* Set Contiguous * Set Contiguous
* *
@ -403,6 +491,7 @@ class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
return $this; return $this;
} // function setInputEncoding() } // function setInputEncoding()
/** /**
* Get Contiguous * Get Contiguous
* *

View File

@ -80,6 +80,14 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
private $_charSet = 'UTF-8'; private $_charSet = 'UTF-8';
/**
* Create a new PHPExcel_Reader_Excel2003XML
*/
public function __construct() {
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
}
/** /**
* Read data only? * Read data only?
* *
@ -89,6 +97,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $this->_readDataOnly; return $this->_readDataOnly;
} }
/** /**
* Set read data only * Set read data only
* *
@ -100,6 +109,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/** /**
* Get which sheets to load * Get which sheets to load
* *
@ -110,6 +120,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $this->_loadSheetsOnly; return $this->_loadSheetsOnly;
} }
/** /**
* Set which sheets to load * Set which sheets to load
* *
@ -123,6 +134,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/** /**
* Set all sheets to load * Set all sheets to load
* *
@ -134,6 +146,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/** /**
* Read filter * Read filter
* *
@ -143,6 +156,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $this->_readFilter; return $this->_readFilter;
} }
/** /**
* Set read filter * Set read filter
* *
@ -154,12 +168,6 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/**
* Create a new PHPExcel_Reader_Excel2003XML
*/
public function __construct() {
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
}
/** /**
* Can the current PHPExcel_Reader_IReader read the file? * Can the current PHPExcel_Reader_IReader read the file?
@ -171,15 +179,15 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
public function canRead($pFilename) public function canRead($pFilename)
{ {
// Office xmlns:o="urn:schemas-microsoft-com:office:office" // Office xmlns:o="urn:schemas-microsoft-com:office:office"
// Excel xmlns:x="urn:schemas-microsoft-com:office:excel" // Excel xmlns:x="urn:schemas-microsoft-com:office:excel"
// XML Spreadsheet xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" // XML Spreadsheet xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
// Spreadsheet component xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet" // Spreadsheet component xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet"
// XML schema xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" // XML schema xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
// XML data type xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" // XML data type xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
// MS-persist recordset xmlns:rs="urn:schemas-microsoft-com:rowset" // MS-persist recordset xmlns:rs="urn:schemas-microsoft-com:rowset"
// Rowset xmlns:z="#RowsetSchema" // Rowset xmlns:z="#RowsetSchema"
// //
$signature = array( $signature = array(
'<?xml version="1.0"', '<?xml version="1.0"',
@ -214,6 +222,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $valid; return $valid;
} }
/** /**
* Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
* *
@ -246,6 +255,77 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
/** /**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
*
* @param string $pFilename
* @throws Exception
*/
public function listWorksheetInfo($pFilename)
{
// Check if file exists
if (!file_exists($pFilename)) {
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
$worksheetInfo = array();
$xml = simplexml_load_file($pFilename);
$namespaces = $xml->getNamespaces(true);
$worksheetID = 1;
$xml_ss = $xml->children($namespaces['ss']);
foreach($xml_ss->Worksheet as $worksheet) {
$worksheet_ss = $worksheet->attributes($namespaces['ss']);
$tmpInfo = array();
$tmpInfo['worksheetName'] = '';
$tmpInfo['lastColumnLetter'] = 'A';
$tmpInfo['lastColumnIndex'] = 0;
$tmpInfo['totalRows'] = 0;
$tmpInfo['totalColumns'] = 0;
if (isset($worksheet_ss['Name'])) {
$tmpInfo['worksheetName'] = (string) $worksheet_ss['Name'];
} else {
$tmpInfo['worksheetName'] = "Worksheet_{$worksheetID}";
}
if (isset($worksheet->Table->Row)) {
$rowIndex = 0;
foreach($worksheet->Table->Row as $rowData) {
$columnIndex = 0;
$rowHasData = false;
foreach($rowData->Cell as $cell) {
if (isset($cell->Data)) {
$tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
$rowHasData = true;
}
++$columnIndex;
}
++$rowIndex;
if ($rowHasData) {
$tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
}
}
}
$tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
$tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
$worksheetInfo[] = $tmpInfo;
++$worksheetID;
}
return $worksheetInfo;
}
/**
* Loads PHPExcel from file * Loads PHPExcel from file
* *
* @param string $pFilename * @param string $pFilename
@ -261,6 +341,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $this->loadIntoExisting($pFilename, $objPHPExcel); return $this->loadIntoExisting($pFilename, $objPHPExcel);
} }
private static function identifyFixedStyleValue($styleList,&$styleAttributeValue) { private static function identifyFixedStyleValue($styleList,&$styleAttributeValue) {
$styleAttributeValue = strtolower($styleAttributeValue); $styleAttributeValue = strtolower($styleAttributeValue);
foreach($styleList as $style) { foreach($styleList as $style) {
@ -272,6 +353,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return false; return false;
} }
/** /**
* pixel units to excel width units(units of 1/256th of a character width) * pixel units to excel width units(units of 1/256th of a character width)
* @param pxs * @param pxs
@ -285,6 +367,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $widthUnits; return $widthUnits;
} }
/** /**
* excel width units(units of 1/256th of a character width) to pixel units * excel width units(units of 1/256th of a character width) to pixel units
* @param widthUnits * @param widthUnits
@ -302,6 +385,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return chr(hexdec($hex[1])); return chr(hexdec($hex[1]));
} }
/** /**
* Loads PHPExcel from file into PHPExcel instance * Loads PHPExcel from file into PHPExcel instance
* *
@ -802,6 +886,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $objPHPExcel; return $objPHPExcel;
} }
private static function _convertStringEncoding($string,$charset) { private static function _convertStringEncoding($string,$charset) {
if ($charset != 'UTF-8') { if ($charset != 'UTF-8') {
return PHPExcel_Shared_String::ConvertEncoding($string,'UTF-8',$charset); return PHPExcel_Shared_String::ConvertEncoding($string,'UTF-8',$charset);
@ -809,6 +894,7 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
return $string; return $string;
} }
private function _parseRichText($is = '') { private function _parseRichText($is = '') {
$value = new PHPExcel_RichText(); $value = new PHPExcel_RichText();

View File

@ -91,6 +91,15 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
private static $_theme = null; private static $_theme = null;
/**
* Create a new PHPExcel_Reader_Excel2007 instance
*/
public function __construct() {
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
$this->_referenceHelper = PHPExcel_ReferenceHelper::getInstance();
}
/** /**
* Read data only? * Read data only?
* If this is true, then the Reader will only read data values for cells, it will not read any formatting information. * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
@ -102,6 +111,7 @@ class PHPExcel_Reader_Excel2007 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.
@ -115,6 +125,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
$this->_readDataOnly = $pValue; $this->_readDataOnly = $pValue;
return $this; return $this;
} }
/** /**
* Read charts in workbook? * Read charts in workbook?
@ -128,6 +139,7 @@ class PHPExcel_Reader_Excel2007 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.
@ -143,6 +155,7 @@ class PHPExcel_Reader_Excel2007 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
@ -155,6 +168,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
return $this->_loadSheetsOnly; return $this->_loadSheetsOnly;
} }
/** /**
* Set which sheets to load * Set which sheets to load
* *
@ -171,6 +185,7 @@ class PHPExcel_Reader_Excel2007 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.
@ -183,6 +198,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/** /**
* Read filter * Read filter
* *
@ -192,6 +208,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
return $this->_readFilter; return $this->_readFilter;
} }
/** /**
* Set read filter * Set read filter
* *
@ -203,13 +220,6 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/**
* Create a new PHPExcel_Reader_Excel2007 instance
*/
public function __construct() {
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
$this->_referenceHelper = PHPExcel_ReferenceHelper::getInstance();
}
/** /**
* Can the current PHPExcel_Reader_IReader read the file? * Can the current PHPExcel_Reader_IReader read the file?
@ -254,6 +264,82 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
return $xl; return $xl;
} }
/**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
*
* @param string $pFilename
* @throws Exception
*/
public function listWorksheetInfo($pFilename)
{
// Check if file exists
if (!file_exists($pFilename)) {
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
$worksheetInfo = array();
$zip = new ZipArchive;
$zip->open($pFilename);
$rels = simplexml_load_string($this->_getFromZipArchive($zip, "_rels/.rels")); //~ http://schemas.openxmlformats.org/package/2006/relationships");
foreach ($rels->Relationship as $rel) {
if ($rel["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument") {
$dir = dirname($rel["Target"]);
$relsWorkbook = simplexml_load_string($this->_getFromZipArchive($zip, "$dir/_rels/" . basename($rel["Target"]) . ".rels")); //~ http://schemas.openxmlformats.org/package/2006/relationships");
$relsWorkbook->registerXPathNamespace("rel", "http://schemas.openxmlformats.org/package/2006/relationships");
$worksheets = array();
foreach ($relsWorkbook->Relationship as $ele) {
if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet") {
$worksheets[(string) $ele["Id"]] = $ele["Target"];
}
}
$xmlWorkbook = simplexml_load_string($this->_getFromZipArchive($zip, "{$rel['Target']}")); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
if ($xmlWorkbook->sheets) {
$dir = dirname($rel["Target"]);
foreach ($xmlWorkbook->sheets->sheet as $eleSheet) {
$tmpInfo = array();
$tmpInfo['worksheetName'] = (string) $eleSheet["name"];
$tmpInfo['lastColumnLetter'] = 'A';
$tmpInfo['lastColumnIndex'] = 0;
$tmpInfo['totalRows'] = 0;
$tmpInfo['totalColumns'] = 0;
$fileWorksheet = $worksheets[(string) self::array_item($eleSheet->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), "id")];
$xmlSheet = simplexml_load_string($this->_getFromZipArchive($zip, "$dir/$fileWorksheet")); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
if ($xmlSheet && $xmlSheet->sheetData && $xmlSheet->sheetData->row) {
foreach ($xmlSheet->sheetData->row as $row) {
foreach ($row->c as $c) {
$r = (string) $c["r"];
$coordinates = PHPExcel_Cell::coordinateFromString($r);
$rowIndex = $coordinates[1];
$columnIndex = PHPExcel_Cell::columnIndexFromString($coordinates[0]) - 1;
$tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
$tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
}
}
}
$tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
$tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
$worksheetInfo[] = $tmpInfo;
}
}
}
}
$zip->close();
return $worksheetInfo;
}
private static function _castToBool($c) { private static function _castToBool($c) {
// 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;
@ -267,16 +353,19 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
return $value; return $value;
} // function _castToBool() } // function _castToBool()
private static function _castToError($c) { private static function _castToError($c) {
// echo 'Initial Cast to Error<br />'; // echo 'Initial Cast to Error<br />';
return isset($c->v) ? (string) $c->v : null;; return isset($c->v) ? (string) $c->v : null;;
} // function _castToError() } // function _castToError()
private static function _castToString($c) { private static function _castToString($c) {
// echo 'Initial Cast to String<br />'; // echo 'Initial Cast to String<br />';
return isset($c->v) ? (string) $c->v : null;; return isset($c->v) ? (string) $c->v : null;;
} // function _castToString() } // function _castToString()
private function _castToFormula($c,$r,&$cellDataType,&$value,&$calculatedValue,&$sharedFormulas,$castBaseType) { private function _castToFormula($c,$r,&$cellDataType,&$value,&$calculatedValue,&$sharedFormulas,$castBaseType) {
// echo '<font color="darkgreen">Formula</font><br />'; // echo '<font color="darkgreen">Formula</font><br />';
// echo '$c->f is '.$c->f.'<br />'; // echo '$c->f is '.$c->f.'<br />';
@ -325,6 +414,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
} }
} }
public function _getFromZipArchive(ZipArchive $archive, $fileName = '') public function _getFromZipArchive(ZipArchive $archive, $fileName = '')
{ {
// Root-relative paths // Root-relative paths
@ -344,6 +434,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
return $contents; return $contents;
} }
/** /**
* Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
* *
@ -1639,6 +1730,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
return $excel; return $excel;
} }
private static function _readColor($color, $background=false) { private static function _readColor($color, $background=false) {
if (isset($color["rgb"])) { if (isset($color["rgb"])) {
@ -1662,6 +1754,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
return 'FF000000'; return 'FF000000';
} }
private static function _readStyle($docStyle, $style) { private static function _readStyle($docStyle, $style) {
// format code // format code
if (isset($style->numFmt)) { if (isset($style->numFmt)) {
@ -1788,6 +1881,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
} }
} }
} }
private static function _readBorder($docBorder, $eleBorder) { private static function _readBorder($docBorder, $eleBorder) {
if (isset($eleBorder["style"])) { if (isset($eleBorder["style"])) {
@ -1798,6 +1892,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
} }
} }
private function _parseRichText($is = null) { private function _parseRichText($is = null) {
$value = new PHPExcel_RichText(); $value = new PHPExcel_RichText();
@ -1860,14 +1955,17 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
return $value; return $value;
} }
private static function array_item($array, $key = 0) { private static function array_item($array, $key = 0) {
return (isset($array[$key]) ? $array[$key] : null); return (isset($array[$key]) ? $array[$key] : null);
} }
private static function dir_add($base, $add) { private static function dir_add($base, $add) {
return preg_replace('~[^/]+/\.\./~', '', dirname($base) . "/$add"); return preg_replace('~[^/]+/\.\./~', '', dirname($base) . "/$add");
} }
private static function toCSSArray($style) { private static function toCSSArray($style) {
$style = str_replace(array("\r","\n"), "", $style); $style = str_replace(array("\r","\n"), "", $style);

File diff suppressed because it is too large Load Diff

View File

@ -85,6 +85,15 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
private $_readFilter = null; private $_readFilter = null;
/**
* Create a new PHPExcel_Reader_Gnumeric
*/
public function __construct() {
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
$this->_referenceHelper = PHPExcel_ReferenceHelper::getInstance();
}
/** /**
* Read data only? * Read data only?
* If this is true, then the Reader will only read data values for cells, it will not read any formatting information. * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
@ -96,6 +105,7 @@ class PHPExcel_Reader_Gnumeric 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.
@ -110,6 +120,7 @@ class PHPExcel_Reader_Gnumeric 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
@ -122,6 +133,7 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
return $this->_loadSheetsOnly; return $this->_loadSheetsOnly;
} }
/** /**
* Set which sheets to load * Set which sheets to load
* *
@ -138,6 +150,7 @@ class PHPExcel_Reader_Gnumeric 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.
@ -150,6 +163,7 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/** /**
* Read filter * Read filter
* *
@ -159,6 +173,7 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
return $this->_readFilter; return $this->_readFilter;
} }
/** /**
* Set read filter * Set read filter
* *
@ -170,13 +185,6 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/**
* Create a new PHPExcel_Reader_Gnumeric
*/
public function __construct() {
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
$this->_referenceHelper = PHPExcel_ReferenceHelper::getInstance();
}
/** /**
* Can the current PHPExcel_Reader_IReader read the file? * Can the current PHPExcel_Reader_IReader read the file?
@ -209,6 +217,70 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
return true; return true;
} }
/**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
*
* @param string $pFilename
* @throws Exception
*/
public function listWorksheetInfo($pFilename)
{
// Check if file exists
if (!file_exists($pFilename)) {
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
$gFileData = $this->_gzfileGetContents($pFilename);
$xml = simplexml_load_string($gFileData);
$namespacesMeta = $xml->getNamespaces(true);
$gnmXML = $xml->children($namespacesMeta['gnm']);
$worksheetInfo = array();
foreach ($gnmXML->Sheets->Sheet as $sheet) {
$tmpInfo = array();
$tmpInfo['worksheetName'] = (string) $sheet->Name;
$tmpInfo['lastColumnLetter'] = 'A';
$tmpInfo['lastColumnIndex'] = 0;
$tmpInfo['totalRows'] = 0;
$tmpInfo['totalColumns'] = 0;
foreach ($sheet->Cells->Cell as $cell) {
$cellAttributes = $cell->attributes();
$rowIndex = (int) $cellAttributes->Row + 1;
$columnIndex = (int) $cellAttributes->Col;
$tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
$tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
}
$tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
$tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
$worksheetInfo[] = $tmpInfo;
}
return $worksheetInfo;
}
private function _gzfileGetContents($filename) {
$file = @gzopen($filename, 'rb');
if ($file !== false) {
$data = '';
while (!gzeof($file)) {
$data .= gzread($file, 1024);
}
gzclose($file);
}
return $data;
}
/** /**
* Loads PHPExcel from file * Loads PHPExcel from file
* *
@ -225,17 +297,6 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
return $this->loadIntoExisting($pFilename, $objPHPExcel); return $this->loadIntoExisting($pFilename, $objPHPExcel);
} }
private function _gzfileGetContents($filename) {
$file = @gzopen($filename, 'rb');
if ($file !== false) {
$data = '';
while (!gzeof($file)) {
$data .= gzread($file, 1024);
}
gzclose($file);
}
return $data;
}
/** /**
* Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
@ -839,6 +900,7 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
return $objPHPExcel; return $objPHPExcel;
} }
private static function _parseBorderAttributes($borderAttributes) { private static function _parseBorderAttributes($borderAttributes) {
$styleArray = array(); $styleArray = array();
@ -894,6 +956,7 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
return $styleArray; return $styleArray;
} }
private function _parseRichText($is = '') { private function _parseRichText($is = '') {
$value = new PHPExcel_RichText(); $value = new PHPExcel_RichText();
@ -902,6 +965,7 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
return $value; return $value;
} }
private static function _parseGnumericColour($gnmColour) { private static function _parseGnumericColour($gnmColour) {
list($gnmR,$gnmG,$gnmB) = explode(':',$gnmColour); list($gnmR,$gnmG,$gnmB) = explode(':',$gnmColour);
$gnmR = substr(str_pad($gnmR,4,'0',STR_PAD_RIGHT),0,2); $gnmR = substr(str_pad($gnmR,4,'0',STR_PAD_RIGHT),0,2);

View File

@ -76,6 +76,14 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
private $_readFilter = null; private $_readFilter = null;
/**
* Create a new PHPExcel_Reader_OOCalc
*/
public function __construct() {
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
}
/** /**
* Read data only? * Read data only?
* If this is true, then the Reader will only read data values for cells, it will not read any formatting information. * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
@ -87,6 +95,7 @@ class PHPExcel_Reader_OOCalc 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.
@ -100,6 +109,7 @@ class PHPExcel_Reader_OOCalc 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
@ -112,6 +122,7 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
return $this->_loadSheetsOnly; return $this->_loadSheetsOnly;
} }
/** /**
* Set which sheets to load * Set which sheets to load
* *
@ -128,6 +139,7 @@ class PHPExcel_Reader_OOCalc 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.
@ -140,6 +152,7 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/** /**
* Read filter * Read filter
* *
@ -149,6 +162,7 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
return $this->_readFilter; return $this->_readFilter;
} }
/** /**
* Set read filter * Set read filter
* *
@ -160,12 +174,6 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
return $this; return $this;
} }
/**
* Create a new PHPExcel_Reader_OOCalc
*/
public function __construct() {
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
}
/** /**
* Can the current PHPExcel_Reader_IReader read the file? * Can the current PHPExcel_Reader_IReader read the file?
@ -200,6 +208,7 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
return false; return false;
} }
/** /**
* Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
* *
@ -252,6 +261,7 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
return $this->loadIntoExisting($pFilename, $objPHPExcel); return $this->loadIntoExisting($pFilename, $objPHPExcel);
} }
private static function identifyFixedStyleValue($styleList,&$styleAttributeValue) { private static function identifyFixedStyleValue($styleList,&$styleAttributeValue) {
$styleAttributeValue = strtolower($styleAttributeValue); $styleAttributeValue = strtolower($styleAttributeValue);
foreach($styleList as $style) { foreach($styleList as $style) {
@ -263,6 +273,97 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
return false; return false;
} }
/**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
*
* @param string $pFilename
* @throws Exception
*/
public function listWorksheetInfo($pFilename)
{
// Check if file exists
if (!file_exists($pFilename)) {
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
$worksheetInfo = array();
$zip = new ZipArchive;
if ($zip->open($pFilename) === true) {
$xml = simplexml_load_string($zip->getFromName("content.xml"));
$namespacesContent = $xml->getNamespaces(true);
$workbook = $xml->children($namespacesContent['office']);
foreach($workbook->body->spreadsheet as $workbookData) {
$workbookData = $workbookData->children($namespacesContent['table']);
foreach($workbookData->table as $worksheetDataSet) {
$worksheetData = $worksheetDataSet->children($namespacesContent['table']);
$worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
$tmpInfo = array();
$tmpInfo['worksheetName'] = $worksheetDataAttributes['name'];
$tmpInfo['lastColumnLetter'] = 'A';
$tmpInfo['lastColumnIndex'] = 0;
$tmpInfo['totalRows'] = 0;
$tmpInfo['totalColumns'] = 0;
$rowIndex = 0;
foreach ($worksheetData as $key => $rowData) {
$rowHasData = false;
switch ($key) {
case 'table-row' :
$columnIndex = 0;
foreach ($rowData as $key => $cellData) {
$cellHasData = false;
$cellDataText = $cellData->children($namespacesContent['text']);
$cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
if (isset($cellDataText->p)) {
switch ($cellDataOfficeAttributes['value-type']) {
case 'string' :
case 'boolean' :
case 'float' :
case 'date' :
case 'time' :
$cellHasData = true;
break;
}
}
$cellDataText = null;
$cellDataOfficeAttributes = null;
if ($cellHasData) {
$tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
$rowHasData = true;
}
++$columnIndex;
}
++$rowIndex;
if ($rowHasData) {
$tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
}
break;
}
}
$tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
$tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
$worksheetInfo[] = $tmpInfo;
}
}
}
return $worksheetInfo;
}
/** /**
* Loads PHPExcel from file into PHPExcel instance * Loads PHPExcel from file into PHPExcel instance
* *
@ -589,6 +690,7 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
return $objPHPExcel; return $objPHPExcel;
} }
private function _parseRichText($is = '') { private function _parseRichText($is = '') {
$value = new PHPExcel_RichText(); $value = new PHPExcel_RichText();

View File

@ -79,6 +79,7 @@ class PHPExcel_Reader_SYLK implements PHPExcel_Reader_IReader
*/ */
private $_readFilter = null; private $_readFilter = null;
/** /**
* Create a new PHPExcel_Reader_SYLK * Create a new PHPExcel_Reader_SYLK
*/ */
@ -86,6 +87,7 @@ class PHPExcel_Reader_SYLK implements PHPExcel_Reader_IReader
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter(); $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
} }
/** /**
* Can the current PHPExcel_Reader_IReader read the file? * Can the current PHPExcel_Reader_IReader read the file?
* *
@ -120,6 +122,123 @@ class PHPExcel_Reader_SYLK implements PHPExcel_Reader_IReader
return true; return true;
} }
/**
* Read filter
*
* @return PHPExcel_Reader_IReadFilter
*/
public function getReadFilter() {
return $this->_readFilter;
}
/**
* Set read filter
*
* @param PHPExcel_Reader_IReadFilter $pValue
*/
public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
$this->_readFilter = $pValue;
return $this;
}
/**
* Set input encoding
*
* @param string $pValue Input encoding
*/
public function setInputEncoding($pValue = 'ANSI')
{
$this->_inputEncoding = $pValue;
return $this;
}
/**
* Get input encoding
*
* @return string
*/
public function getInputEncoding()
{
return $this->_inputEncoding;
}
/**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
*
* @param string $pFilename
* @throws Exception
*/
public function listWorksheetInfo($pFilename)
{
// Check if file exists
if (!file_exists($pFilename)) {
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
// Open file
$fileHandle = fopen($pFilename, 'r');
if ($fileHandle === false) {
throw new Exception("Could not open file " . $pFilename . " for reading.");
}
$worksheetInfo = array();
$worksheetInfo[0]['worksheetName'] = 'Worksheet';
$worksheetInfo[0]['lastColumnLetter'] = 'A';
$worksheetInfo[0]['lastColumnIndex'] = 0;
$worksheetInfo[0]['totalRows'] = 0;
$worksheetInfo[0]['totalColumns'] = 0;
// Loop through file
$rowData = array();
// loop through one row (line) at a time in the file
$rowIndex = 0;
while (($rowData = fgets($fileHandle)) !== FALSE) {
$columnIndex = 0;
// convert SYLK encoded $rowData to UTF-8
$rowData = PHPExcel_Shared_String::SYLKtoUTF8($rowData);
// explode each row at semicolons while taking into account that literal semicolon (;)
// is escaped like this (;;)
$rowData = explode("\t",str_replace('?',';',str_replace(';',"\t",str_replace(';;','?',rtrim($rowData)))));
$dataType = array_shift($rowData);
if ($dataType == 'C') {
// Read cell value data
foreach($rowData as $rowDatum) {
switch($rowDatum{0}) {
case 'C' :
case 'X' :
$columnIndex = substr($rowDatum,1) - 1;
break;
case 'R' :
case 'Y' :
$rowIndex = substr($rowDatum,1);
break;
}
$worksheetInfo[0]['totalRows'] = max($worksheetInfo[0]['totalRows'], $rowIndex);
$worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], $columnIndex);
}
}
}
$worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
$worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
// Close file
fclose($fileHandle);
return $worksheetInfo;
}
/** /**
* Loads PHPExcel from file * Loads PHPExcel from file
* *
@ -136,45 +255,6 @@ class PHPExcel_Reader_SYLK implements PHPExcel_Reader_IReader
return $this->loadIntoExisting($pFilename, $objPHPExcel); return $this->loadIntoExisting($pFilename, $objPHPExcel);
} }
/**
* Read filter
*
* @return PHPExcel_Reader_IReadFilter
*/
public function getReadFilter() {
return $this->_readFilter;
}
/**
* Set read filter
*
* @param PHPExcel_Reader_IReadFilter $pValue
*/
public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
$this->_readFilter = $pValue;
return $this;
}
/**
* Set input encoding
*
* @param string $pValue Input encoding
*/
public function setInputEncoding($pValue = 'ANSI')
{
$this->_inputEncoding = $pValue;
return $this;
}
/**
* Get input encoding
*
* @return string
*/
public function getInputEncoding()
{
return $this->_inputEncoding;
}
/** /**
* Loads PHPExcel from file into PHPExcel instance * Loads PHPExcel from file into PHPExcel instance
@ -393,6 +473,7 @@ class PHPExcel_Reader_SYLK implements PHPExcel_Reader_IReader
return $objPHPExcel; return $objPHPExcel;
} }
/** /**
* Get sheet index * Get sheet index
* *
@ -402,6 +483,7 @@ class PHPExcel_Reader_SYLK implements PHPExcel_Reader_IReader
return $this->_sheetIndex; return $this->_sheetIndex;
} }
/** /**
* Set sheet index * Set sheet index
* *