From 14cc95232301a06a98b2f0c9c0f5cdd4525b3571 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Tue, 28 Dec 2010 19:34:21 +0000 Subject: [PATCH] Feature: Work item 14979 - Added listWorksheetNames() method to OOCalc and Gnumeric Readers git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@66079 2327b42d-5241-43d6-9e2a-de5ac946f064 --- Classes/PHPExcel/Reader/Gnumeric.php | 30 +++++++++++++++++++++++ Classes/PHPExcel/Reader/OOCalc.php | 36 ++++++++++++++++++++++++++++ changelog.txt | 2 +- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/Classes/PHPExcel/Reader/Gnumeric.php b/Classes/PHPExcel/Reader/Gnumeric.php index aae491dd..f0863bca 100644 --- a/Classes/PHPExcel/Reader/Gnumeric.php +++ b/Classes/PHPExcel/Reader/Gnumeric.php @@ -222,6 +222,36 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader return $data; } + /** + * 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."); + } + + $gFileData = $this->_gzfileGetContents($pFilename); + + $xml = simplexml_load_string($gFileData); + $namespacesMeta = $xml->getNamespaces(true); + + $gnmXML = $xml->children($namespacesMeta['gnm']); + + $worksheetNames = array(); + + foreach($gnmXML->Sheets->Sheet as $sheet) { + $worksheetNames[] = (string) $sheet->Name; + } + + return $worksheetNames; + } + + /** * Loads PHPExcel from file into PHPExcel instance * diff --git a/Classes/PHPExcel/Reader/OOCalc.php b/Classes/PHPExcel/Reader/OOCalc.php index 8afdf42a..20cd745d 100644 --- a/Classes/PHPExcel/Reader/OOCalc.php +++ b/Classes/PHPExcel/Reader/OOCalc.php @@ -186,6 +186,42 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader return false; } + /** + * 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; + 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) { + $worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']); + + $worksheetNames[] = $worksheetDataAttributes['name']; + } + } + } + + return $worksheetNames; + } + + /** * Loads PHPExcel from file * diff --git a/changelog.txt b/changelog.txt index 3cb0f760..a81585f9 100644 --- a/changelog.txt +++ b/changelog.txt @@ -26,7 +26,7 @@ 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. +- Feature: (MBaker) Work item 14979 - Added listWorksheetNames() method to Excel2007, Excel5, OOCalc and Gnumeric 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