Feature: Work item 14979 - Added listWorksheetNames() method to Excel2007 and Excel5 Readers, allowing a user to extract a list of worksheet names from a file without parsing/loading the whole file.

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@66063 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2010-12-28 18:46:23 +00:00
parent 457f826fa6
commit e3a01db2a9
3 changed files with 92 additions and 8 deletions

View File

@ -277,17 +277,48 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
$contents = $archive->getFromName(substr($fileName, 1));
}
/*
if (strpos($contents, '<?xml') !== false && strpos($contents, '<?xml') !== 0)
{
$contents = substr($contents, strpos($contents, '<?xml'));
}
var_dump($fileName);
var_dump($contents);
*/
return $contents;
}
/**
* Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
*
* @param string $pFilename
* @throws Exception
*/
public function listWorksheetNames($pFilename)
{
// Check if file exists
if (!file_exists($pFilename)) {
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
$worksheetNames = 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) {
switch ($rel["Type"]) {
case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument":
$xmlWorkbook = simplexml_load_string($this->_getFromZipArchive($zip, "{$rel['Target']}")); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
if ($xmlWorkbook->sheets) {
foreach ($xmlWorkbook->sheets->sheet as $eleSheet) {
// Check if sheet should be skipped
$worksheetNames[] = (string) $eleSheet["name"];
}
}
}
}
$zip->close();
return $worksheetNames;
}
/**
* Loads PHPExcel from file
*
@ -1428,6 +1459,8 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
}
$zip->close();
return $excel;
}

View File

@ -493,6 +493,55 @@ class PHPExcel_Reader_Excel5 implements PHPExcel_Reader_IReader
}
}
/**
* Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
*
* @param string $pFilename
* @throws Exception
*/
public function listWorksheetNames($pFilename)
{
// Check if file exists
if (!file_exists($pFilename)) {
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}
$worksheetNames = array();
// Read the OLE file
$this->_loadOLE($pFilename);
// total byte size of Excel data (workbook global substream + sheet substreams)
$this->_dataSize = strlen($this->_data);
$this->_pos = 0;
$this->_sheets = array();
// Parse Workbook Global Substream
while ($this->_pos < $this->_dataSize) {
$code = self::_GetInt2d($this->_data, $this->_pos);
switch ($code) {
case self::XLS_Type_BOF: $this->_readBof(); break;
case self::XLS_Type_SHEET: $this->_readSheet(); break;
case self::XLS_Type_EOF: $this->_readDefault(); break 2;
default: $this->_readDefault(); break;
}
}
foreach ($this->_sheets as $sheet) {
if ($sheet['sheetType'] != 0x00) {
// 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
continue;
}
$worksheetNames[] = $sheet['name'];
}
return $worksheetNames;
}
/**
* Loads PHPExcel from file
*

View File

@ -26,12 +26,14 @@
Fixed in SVN:
- Feature: (MBaker) Provide option to use PCLZip as an alternative to ZipArchive.
This allows the writing of Excel2007 files, even without ZipArchive enabled (it does require zlib), or when php_zip is one of the buggy PHP 5.2.6 or 5.2.8 versions
- Feature: (MBaker) Work item 14979 - Added listWorksheetNames() method to Excel2007 and Excel5 Readers, allowing a user to extract a list of worksheet names from a file without parsing/loading the whole file.
- Bugfix: (MBaker) Work item 14888 - Simple =IF() formula disappears
- Bugfix: (MBaker) Work item 14898 - PHP Warning: preg_match(): Compilation failed: PCRE does not support \\L, \\l, \\N, \\P, \\p, \\U, \\u, or \\X
- Bugfix: (MBaker) Work item 14901 - VLOOKUP choking on parameters in PHPExcel.1.7.5/PHPExcel_Writer_Excel2007
- Bugfix: (MBaker) Work item 14973 - PHPExcel_Cell::isInRange() incorrect results - offset by one column
2010-12-10 (v1.7.5):
- Feature: (MBaker) Work item 8769 - Implement Gnumeric File Format
Initial work on Gnumeric Reader (Worksheet Data, Document Properties and basic Formatting)