New examples of Reading Workbooks

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@62680 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2010-10-23 13:31:14 +00:00
parent 14dbe6ec72
commit 22f704e4b1
3 changed files with 148 additions and 45 deletions

View File

@ -11,7 +11,7 @@
<body> <body>
<h1>PHPExcel Reader Example #14</h1> <h1>PHPExcel Reader Example #14</h1>
<h2>Simple File Reader for Tab-Separated Value File using the Advanced Value Binder</h2> <h2>Reading a Large CSV file in "Chunks" to split across multiple Worksheets</h2>
<?php <?php
/** Include path **/ /** Include path **/
@ -21,17 +21,69 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
include 'PHPExcel/IOFactory.php'; include 'PHPExcel/IOFactory.php';
PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
$inputFileType = 'CSV'; $inputFileType = 'CSV';
$inputFileName = './sampleData/example1.tsv'; $inputFileName = './sampleData/example2.csv';
/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
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); echo '<hr />';
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
/** 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),'<br />';
/** 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 '<hr />'; echo '<hr />';
@ -39,27 +91,7 @@ echo '<hr />';
echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />'; echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />';
$loadedSheetNames = $objPHPExcel->getSheetNames(); $loadedSheetNames = $objPHPExcel->getSheetNames();
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Formatted)</b><br />'; echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,'</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); $objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,false,false,true); $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,false,false,true);
var_dump($sheetData); var_dump($sheetData);

View File

@ -1,10 +1,5 @@
<?php <?php
error_reporting(E_ALL);
set_time_limit(0);
date_default_timezone_set('Europe/London');
?> ?>
<html> <html>
<head> <head>
@ -16,7 +11,7 @@ date_default_timezone_set('Europe/London');
<body> <body>
<h1>PHPExcel Reader Example #15</h1> <h1>PHPExcel Reader Example #15</h1>
<h2>Handling Loader Exceptions using Try/Catch</h2> <h2>Simple File Reader for Tab-Separated Value File using the Advanced Value Binder</h2>
<?php <?php
/** Include path **/ /** Include path **/
@ -26,20 +21,50 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
include 'PHPExcel/IOFactory.php'; include 'PHPExcel/IOFactory.php';
$inputFileName = './sampleData/example_1.xls'; PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />';
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName); $inputFileType = 'CSV';
} catch(Exception $e) { $inputFileName = './sampleData/example1.tsv';
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
} $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();
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo '<b>Worksheet #',$sheetIndex,' -> ',$loadedSheetName,' (Formatted)</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 />';
}
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

@ -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 #16</title>
</head>
<body>
<h1>PHPExcel Reader Example #16</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>