diff --git a/Documentation/Examples/Reader/exampleReader11.php b/Documentation/Examples/Reader/exampleReader11.php index c1c1fc5..becdb63 100644 --- a/Documentation/Examples/Reader/exampleReader11.php +++ b/Documentation/Examples/Reader/exampleReader11.php @@ -16,7 +16,7 @@ date_default_timezone_set('Europe/London');

PHPExcel Reader Example #11

-

Simple File Reader for Multiple CSV Files

+

Reading a Workbook in "Chunks" Using a Configurable Read Filter (Version 1)

'; -$objPHPExcel = $objReader->load($inputFileName); -$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME)); -foreach($inputFileNames as $sheet => $inputFileName) { - echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' into WorkSheet #',($sheet+2),' using IOFactory with a defined reader type of ',$inputFileType,'
'; - $objReader->setSheetIndex($sheet+1); - $objReader->loadIntoExisting($inputFileName,$objPHPExcel); - $objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME)); + +/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */ +class chunkReadFilter implements PHPExcel_Reader_IReadFilter +{ + private $_startRow = 0; + + private $_endRow = 0; + + /** We expect a list of the rows that we want to read to be passed into the constructor */ + public function __construct($startRow, $chunkSize) { + $this->_startRow = $startRow; + $this->_endRow = $startRow + $chunkSize; + } + + public function readCell($column, $row, $worksheetName = '') { + // Only read the heading row, and the rows that were configured in the constructor + 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 '
'; -echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded

'; -$loadedSheetNames = $objPHPExcel->getSheetNames(); -foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { - echo 'Worksheet #',$sheetIndex,' -> ',$loadedSheetName,'
'; - $objPHPExcel->setActiveSheetIndexByName($loadedSheetName); + +/** Define how many rows we want for each "chunk" **/ +$chunkSize = 20; + +/** Loop to read our worksheet in "chunk size" blocks **/ +for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) { + echo 'Loading WorkSheet using configurable filter for headings row 1 and for rows ',$startRow,' to ',($startRow+$chunkSize-1),'
'; + /** Create a new Instance of our Read Filter, passing in the limits on which rows we want to read **/ + $chunkFilter = new chunkReadFilter($startRow,$chunkSize); + /** Tell the Reader that we want to use the new Read Filter that we've just Instantiated **/ + $objReader->setReadFilter($chunkFilter); + /** Load only the rows that match our filter from $inputFileName to a PHPExcel Object **/ + $objPHPExcel = $objReader->load($inputFileName); + + // Do some processing here + $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); var_dump($sheetData); - echo '
'; + echo '

'; } diff --git a/Documentation/Examples/Reader/exampleReader12.php b/Documentation/Examples/Reader/exampleReader12.php index 82c2c23..7011355 100644 --- a/Documentation/Examples/Reader/exampleReader12.php +++ b/Documentation/Examples/Reader/exampleReader12.php @@ -1,6 +1,9 @@ @@ -13,60 +16,74 @@ include_once('../baseIncludes.php');

PHPExcel Reader Example #12

-

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

+

Reading a Workbook in "Chunks" Using a Configurable Read Filter (Version 2)

_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 '
'; -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); + +/** Define how many rows we want to read for each "chunk" **/ +$chunkSize = 20; +/** 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); + +/** Loop to read our worksheet in "chunk size" blocks **/ +for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) { + echo 'Loading WorkSheet 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); + /** Load only the rows that match our filter from $inputFileName to a PHPExcel Object **/ + $objPHPExcel = $objReader->load($inputFileName); + + // Do some processing here + $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); var_dump($sheetData); - echo '
'; + 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/exampleReader13.php b/Documentation/Examples/Reader/exampleReader13.php index 1b0bfa5..9c9d903 100644 --- a/Documentation/Examples/Reader/exampleReader13.php +++ b/Documentation/Examples/Reader/exampleReader13.php @@ -16,7 +16,7 @@ date_default_timezone_set('Europe/London');

PHPExcel Reader Example #13

-

Handling Loader Exceptions using Try/Catch

+

Simple File Reader for Multiple CSV Files

'; -try { - $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); -} catch(Exception $e) { - die('Error loading file "',pathinfo($inputFileName,PATHINFO_BASENAME),'": ',$e->getMessage()); +$inputFileType = 'CSV'; +$inputFileNames = array('./sampleData/example1.csv','./sampleData/example2.csv'); + +$objReader = PHPExcel_IOFactory::createReader($inputFileType); +$inputFileName = array_shift($inputFileNames); +echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' into WorkSheet #1 using IOFactory with a defined reader type of ',$inputFileType,'
'; +$objPHPExcel = $objReader->load($inputFileName); +$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME)); +foreach($inputFileNames as $sheet => $inputFileName) { + echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' into WorkSheet #',($sheet+2),' using IOFactory with a defined reader type of ',$inputFileType,'
'; + $objReader->setSheetIndex($sheet+1); + $objReader->loadIntoExisting($inputFileName,$objPHPExcel); + $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,'
'; + $objPHPExcel->setActiveSheetIndexByName($loadedSheetName); + $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); + var_dump($sheetData); + echo '
'; +} ?> diff --git a/Documentation/Examples/Reader/exampleReader14.php b/Documentation/Examples/Reader/exampleReader14.php new file mode 100644 index 0000000..f8661b8 --- /dev/null +++ b/Documentation/Examples/Reader/exampleReader14.php @@ -0,0 +1,73 @@ + + + + + +PHPExcel Reader Example #14 + + + + +

PHPExcel Reader Example #14

+

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

+'; +$objReader->setDelimiter("\t"); +$objPHPExcel = $objReader->load($inputFileName); +$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME)); + + +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)
'; + $objPHPExcel->setActiveSheetIndexByName($loadedSheetName); + $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,false,false,true); + var_dump($sheetData); + echo '
'; +} + +?> + + \ No newline at end of file diff --git a/Documentation/Examples/Reader/exampleReader15.php b/Documentation/Examples/Reader/exampleReader15.php new file mode 100644 index 0000000..c507b8a --- /dev/null +++ b/Documentation/Examples/Reader/exampleReader15.php @@ -0,0 +1,46 @@ + + + + + +PHPExcel Reader Example #15 + + + + +

PHPExcel Reader Example #15

+

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 diff --git a/Documentation/Examples/Reader/sampleData/example2.xls b/Documentation/Examples/Reader/sampleData/example2.xls new file mode 100644 index 0000000..9e24c36 Binary files /dev/null and b/Documentation/Examples/Reader/sampleData/example2.xls differ