mirror of
https://github.com/retailcrm/PHPExcel.git
synced 2024-11-29 08:46:03 +03:00
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:
parent
45109e0533
commit
5af4355b78
@ -16,7 +16,7 @@ date_default_timezone_set('Europe/London');
|
||||
<body>
|
||||
|
||||
<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
|
||||
|
||||
/** Include path **/
|
||||
@ -26,32 +26,63 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
|
||||
include 'PHPExcel/IOFactory.php';
|
||||
|
||||
|
||||
$inputFileType = 'CSV';
|
||||
$inputFileNames = array('./sampleData/example1.csv','./sampleData/example2.csv');
|
||||
$inputFileType = 'Excel5';
|
||||
// $inputFileType = 'Excel2007';
|
||||
// $inputFileType = 'Excel2003XML';
|
||||
// $inputFileType = 'OOCalc';
|
||||
// $inputFileType = 'Gnumeric';
|
||||
$inputFileName = './sampleData/example2.xls';
|
||||
|
||||
$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,'<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));
|
||||
|
||||
/** 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,'<br />';
|
||||
/** Create a new Reader of the type defined in $inputFileType **/
|
||||
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
|
||||
|
||||
|
||||
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);
|
||||
|
||||
/** 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),'<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);
|
||||
var_dump($sheetData);
|
||||
echo '<br />';
|
||||
echo '<br /><br />';
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,6 +1,9 @@
|
||||
<?php
|
||||
|
||||
include_once('../baseIncludes.php');
|
||||
error_reporting(E_ALL);
|
||||
set_time_limit(0);
|
||||
|
||||
date_default_timezone_set('Europe/London');
|
||||
|
||||
?>
|
||||
<html>
|
||||
@ -13,60 +16,74 @@ include_once('../baseIncludes.php');
|
||||
<body>
|
||||
|
||||
<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
|
||||
|
||||
/** Include path **/
|
||||
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
|
||||
/** Set Include path to point at the PHPExcel Classes folder **/
|
||||
set_include_path(get_include_path() . PATH_SEPARATOR . '../../PHPExcel/Classes/');
|
||||
|
||||
/** PHPExcel_IOFactory */
|
||||
/** Include PHPExcel_IOFactory **/
|
||||
include 'PHPExcel/IOFactory.php';
|
||||
|
||||
|
||||
PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
|
||||
$inputFileType = 'Excel5';
|
||||
$inputFileName = './sampleData/example2.xls';
|
||||
|
||||
|
||||
$inputFileType = 'CSV';
|
||||
$inputFileName = './sampleData/example1.tsv';
|
||||
/** 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);
|
||||
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);
|
||||
|
||||
/** 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),'<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);
|
||||
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>
|
||||
|
@ -16,7 +16,7 @@ date_default_timezone_set('Europe/London');
|
||||
<body>
|
||||
|
||||
<h1>PHPExcel Reader Example #13</h1>
|
||||
<h2>Handling Loader Exceptions using Try/Catch</h2>
|
||||
<h2>Simple File Reader for Multiple CSV Files</h2>
|
||||
<?php
|
||||
|
||||
/** Include path **/
|
||||
@ -26,19 +26,33 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
|
||||
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());
|
||||
$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,'<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 />';
|
||||
|
||||
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
|
||||
var_dump($sheetData);
|
||||
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);
|
||||
var_dump($sheetData);
|
||||
echo '<br />';
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
|
73
Documentation/Examples/Reader/exampleReader14.php
Normal file
73
Documentation/Examples/Reader/exampleReader14.php
Normal 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>
|
46
Documentation/Examples/Reader/exampleReader15.php
Normal file
46
Documentation/Examples/Reader/exampleReader15.php
Normal 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>
|
BIN
Documentation/Examples/Reader/sampleData/example2.xls
Normal file
BIN
Documentation/Examples/Reader/sampleData/example2.xls
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user