New examples, including reading a larger workbook in chunks to give improved memory usage

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@62489 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2010-10-20 11:30:09 +00:00
parent 45109e0533
commit 5af4355b78
6 changed files with 246 additions and 65 deletions

View File

@ -16,7 +16,7 @@ date_default_timezone_set('Europe/London');
<body> <body>
<h1>PHPExcel Reader Example #11</h1> <h1>PHPExcel Reader Example #11</h1>
<h2>Simple File Reader for Multiple CSV Files</h2> <h2>Reading a Workbook in "Chunks" Using a Configurable Read Filter (Version 1)</h2>
<?php <?php
/** Include path **/ /** Include path **/
@ -26,32 +26,63 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
include 'PHPExcel/IOFactory.php'; include 'PHPExcel/IOFactory.php';
$inputFileType = 'CSV'; $inputFileType = 'Excel5';
$inputFileNames = array('./sampleData/example1.csv','./sampleData/example2.csv'); // $inputFileType = 'Excel2007';
// $inputFileType = 'Excel2003XML';
// $inputFileType = 'OOCalc';
// $inputFileType = 'Gnumeric';
$inputFileName = './sampleData/example2.xls';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$inputFileName = array_shift($inputFileNames); /** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' into WorkSheet #1 using IOFactory with a defined reader type of ',$inputFileType,'<br />'; class chunkReadFilter implements PHPExcel_Reader_IReadFilter
$objPHPExcel = $objReader->load($inputFileName); {
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME)); private $_startRow = 0;
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,'<br />'; private $_endRow = 0;
$objReader->setSheetIndex($sheet+1);
$objReader->loadIntoExisting($inputFileName,$objPHPExcel); /** We expect a list of the rows that we want to read to be passed into the constructor */
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME)); 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,'<br />';
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
echo '<hr />'; echo '<hr />';
echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />';
$loadedSheetNames = $objPHPExcel->getSheetNames(); /** Define how many rows we want for each "chunk" **/
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { $chunkSize = 20;
echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,'</b><br />';
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName); /** 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),'<br />';
/** 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); $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData); var_dump($sheetData);
echo '<br />'; echo '<br /><br />';
} }

View File

@ -1,6 +1,9 @@
<?php <?php
include_once('../baseIncludes.php'); error_reporting(E_ALL);
set_time_limit(0);
date_default_timezone_set('Europe/London');
?> ?>
<html> <html>
@ -13,60 +16,74 @@ include_once('../baseIncludes.php');
<body> <body>
<h1>PHPExcel Reader Example #12</h1> <h1>PHPExcel Reader Example #12</h1>
<h2>Simple File Reader for Tab-Separated Value File using the Advanced Value Binder</h2> <h2>Reading a Workbook in "Chunks" Using a Configurable Read Filter (Version 2)</h2>
<?php <?php
/** Include path **/ /** Set Include path to point at the PHPExcel Classes folder **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/'); set_include_path(get_include_path() . PATH_SEPARATOR . '../../PHPExcel/Classes/');
/** PHPExcel_IOFactory */ /** Include PHPExcel_IOFactory **/
include 'PHPExcel/IOFactory.php'; include 'PHPExcel/IOFactory.php';
PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() ); $inputFileType = 'Excel5';
$inputFileName = './sampleData/example2.xls';
$inputFileType = 'CSV'; /** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
$inputFileName = './sampleData/example1.tsv'; class chunkReadFilter implements PHPExcel_Reader_IReadFilter
{
private $_startRow = 0;
private $_endRow = 0;
/** Set the list of rows that we want to read */
public function setRows($startRow, $chunkSize) {
$this->_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,'<br />';
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = PHPExcel_IOFactory::createReader($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,'<br />';
$objReader->setDelimiter("\t");
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
echo '<hr />'; echo '<hr />';
echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />';
$loadedSheetNames = $objPHPExcel->getSheetNames(); /** Define how many rows we want to read for each "chunk" **/
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { $chunkSize = 20;
echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Formatted)</b><br />'; /** Create a new Instance of our Read Filter **/
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName); $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),'<br />';
/** 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); $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData); var_dump($sheetData);
echo '<br />'; echo '<br /><br />';
} }
echo '<hr />';
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Unformatted)</b><br />';
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,false,true);
var_dump($sheetData);
echo '<br />';
}
echo '<hr />';
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Raw)</b><br />';
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,false,false,true);
var_dump($sheetData);
echo '<br />';
}
?> ?>
<body> <body>

View File

@ -16,7 +16,7 @@ date_default_timezone_set('Europe/London');
<body> <body>
<h1>PHPExcel Reader Example #13</h1> <h1>PHPExcel Reader Example #13</h1>
<h2>Handling Loader Exceptions using Try/Catch</h2> <h2>Simple File Reader for Multiple CSV Files</h2>
<?php <?php
/** Include path **/ /** Include path **/
@ -26,19 +26,33 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
include 'PHPExcel/IOFactory.php'; include 'PHPExcel/IOFactory.php';
$inputFileName = './sampleData/example_1.xls'; $inputFileType = 'CSV';
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />'; $inputFileNames = array('./sampleData/example1.csv','./sampleData/example2.csv');
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType);
} catch(Exception $e) { $inputFileName = array_shift($inputFileNames);
die('Error loading file "',pathinfo($inputFileName,PATHINFO_BASENAME),'": ',$e->getMessage()); echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' into WorkSheet #1 using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$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,'<br />';
$objReader->setSheetIndex($sheet+1);
$objReader->loadIntoExisting($inputFileName,$objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
} }
echo '<hr />'; echo '<hr />';
echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />';
$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,'</b><br />';
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData); var_dump($sheetData);
echo '<br />';
}
?> ?>

View File

@ -0,0 +1,73 @@
<?php
include_once('../baseIncludes.php');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHPExcel Reader Example #14</title>
</head>
<body>
<h1>PHPExcel Reader Example #14</h1>
<h2>Simple File Reader for Tab-Separated Value File using the Advanced Value Binder</h2>
<?php
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
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,'<br />';
$objReader->setDelimiter("\t");
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
echo '<hr />';
echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />';
$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Formatted)</b><br />';
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
echo '<br />';
}
echo '<hr />';
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Unformatted)</b><br />';
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,false,true);
var_dump($sheetData);
echo '<br />';
}
echo '<hr />';
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Raw)</b><br />';
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,false,false,true);
var_dump($sheetData);
echo '<br />';
}
?>
<body>
</html>

View File

@ -0,0 +1,46 @@
<?php
error_reporting(E_ALL);
set_time_limit(0);
date_default_timezone_set('Europe/London');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHPExcel Reader Example #15</title>
</head>
<body>
<h1>PHPExcel Reader Example #15</h1>
<h2>Handling Loader Exceptions using Try/Catch</h2>
<?php
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
$inputFileName = './sampleData/example_1.xls';
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />';
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch(Exception $e) {
die('Error loading file "',pathinfo($inputFileName,PATHINFO_BASENAME),'": ',$e->getMessage());
}
echo '<hr />';
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
?>
<body>
</html>

Binary file not shown.