From 22f704e4b1484e5abed0c83dc9c205e7cf3060f4 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sat, 23 Oct 2010 13:31:14 +0000 Subject: [PATCH] New examples of Reading Workbooks git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@62680 2327b42d-5241-43d6-9e2a-de5ac946f064 --- .../Examples/Reader/exampleReader14.php | 92 +++++++++++++------ .../Examples/Reader/exampleReader15.php | 55 ++++++++--- .../Examples/Reader/exampleReader16.php | 46 ++++++++++ 3 files changed, 148 insertions(+), 45 deletions(-) create mode 100644 Documentation/Examples/Reader/exampleReader16.php diff --git a/Documentation/Examples/Reader/exampleReader14.php b/Documentation/Examples/Reader/exampleReader14.php index e787ca9..3b19b54 100644 --- a/Documentation/Examples/Reader/exampleReader14.php +++ b/Documentation/Examples/Reader/exampleReader14.php @@ -11,7 +11,7 @@

PHPExcel Reader Example #14

-

Simple File Reader for Tab-Separated Value File using the Advanced Value Binder

+

Reading a Large CSV file in "Chunks" to split across multiple Worksheets

_startRow = $startRow; + $this->_endRow = $startRow + $chunkSize; + } + + public function readCell($column, $row, $worksheetName = '') { + // Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow + if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)) { + return true; + } + return false; + } +} + + +echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'
'; +/** Create a new Reader of the type defined in $inputFileType **/ $objReader = PHPExcel_IOFactory::createReader($inputFileType); -echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' into WorkSheet #1 using IOFactory with a defined reader type of ',$inputFileType,'
'; -$objReader->setDelimiter("\t"); -$objPHPExcel = $objReader->load($inputFileName); -$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME)); + + +echo '
'; + + +/** Define how many rows we want to read for each "chunk" **/ +$chunkSize = 100; +/** Create a new Instance of our Read Filter **/ +$chunkFilter = new chunkReadFilter(); + +/** Tell the Reader that we want to use the Read Filter that we've Instantiated **/ +$objReader->setReadFilter($chunkFilter); + + +/** Instantiate a new PHPExcel object manually **/ +$objPHPExcel = new PHPExcel(); + +/** Set a sheet index **/ +$sheet = 0; +/** Loop to read our worksheet in "chunk size" blocks **/ +/** $startRow is set to 2 initially because we always read the headings in row #1 **/ +for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) { + echo 'Loading WorkSheet #',($sheet+1),' using configurable filter for headings row 1 and for rows ',$startRow,' to ',($startRow+$chunkSize-1),'
'; + /** Tell the Read Filter, the limits on which rows we want to read this iteration **/ + $chunkFilter->setRows($startRow,$chunkSize); + + /** Increment the worksheet index pointer for the Reader **/ + $objReader->setSheetIndex($sheet); + /** Load only the rows that match our filter into a new worksheet in the PHPExcel Object **/ + $objReader->loadIntoExisting($inputFileName,$objPHPExcel); + /** Set the worksheet title (to reference the "sheet" of data that we've loaded) **/ + /** and increment the sheet index as well **/ + $objPHPExcel->getActiveSheet()->setTitle('Country Data #'.(++$sheet)); +} echo '
'; @@ -39,27 +91,7 @@ echo '
'; echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded

'; $loadedSheetNames = $objPHPExcel->getSheetNames(); foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { - echo 'Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Formatted)
'; - $objPHPExcel->setActiveSheetIndexByName($loadedSheetName); - $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); - var_dump($sheetData); - echo '
'; -} - -echo '
'; - -foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { - echo 'Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Unformatted)
'; - $objPHPExcel->setActiveSheetIndexByName($loadedSheetName); - $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,false,true); - var_dump($sheetData); - echo '
'; -} - -echo '
'; - -foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { - echo 'Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Raw)
'; + echo 'Worksheet #',$sheetIndex,' -> ',$loadedSheetName,'
'; $objPHPExcel->setActiveSheetIndexByName($loadedSheetName); $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,false,false,true); var_dump($sheetData); diff --git a/Documentation/Examples/Reader/exampleReader15.php b/Documentation/Examples/Reader/exampleReader15.php index 97aa856..bb68e90 100644 --- a/Documentation/Examples/Reader/exampleReader15.php +++ b/Documentation/Examples/Reader/exampleReader15.php @@ -1,10 +1,5 @@ @@ -16,7 +11,7 @@ date_default_timezone_set('Europe/London');

PHPExcel Reader Example #15

-

Handling Loader Exceptions using Try/Catch

+

Simple File Reader for Tab-Separated Value File using the Advanced Value Binder

'; -try { - $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); -} catch(Exception $e) { - die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); -} +PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() ); + + +$inputFileType = 'CSV'; +$inputFileName = './sampleData/example1.tsv'; + +$objReader = PHPExcel_IOFactory::createReader($inputFileType); +echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' into WorkSheet #1 using IOFactory with a defined reader type of ',$inputFileType,'
'; +$objReader->setDelimiter("\t"); +$objPHPExcel = $objReader->load($inputFileName); +$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME)); echo '
'; -$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); -var_dump($sheetData); +echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded

'; +$loadedSheetNames = $objPHPExcel->getSheetNames(); +foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { + echo 'Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Formatted)
'; + $objPHPExcel->setActiveSheetIndexByName($loadedSheetName); + $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); + var_dump($sheetData); + echo '
'; +} +echo '
'; + +foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { + echo 'Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Unformatted)
'; + $objPHPExcel->setActiveSheetIndexByName($loadedSheetName); + $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,false,true); + var_dump($sheetData); + echo '
'; +} + +echo '
'; + +foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { + echo 'Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Raw)
'; + $objPHPExcel->setActiveSheetIndexByName($loadedSheetName); + $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,false,false,true); + var_dump($sheetData); + echo '
'; +} ?> diff --git a/Documentation/Examples/Reader/exampleReader16.php b/Documentation/Examples/Reader/exampleReader16.php new file mode 100644 index 0000000..0542006 --- /dev/null +++ b/Documentation/Examples/Reader/exampleReader16.php @@ -0,0 +1,46 @@ + + + + + +PHPExcel Reader Example #16 + + + + +

PHPExcel Reader Example #16

+

Handling Loader Exceptions using Try/Catch

+'; +try { + $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); +} catch(Exception $e) { + die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); +} + + +echo '
'; + +$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); +var_dump($sheetData); + + +?> + + \ No newline at end of file