Remove obsolete `$obj` prefix in cod sample

This commit is contained in:
Adrien Crivelli 2016-12-03 22:32:54 +09:00
parent 90da50f3c7
commit 17d1976526
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
24 changed files with 389 additions and 389 deletions

View File

@ -28,14 +28,14 @@ include 'PHPExcel/IOFactory.php';
$inputFileName = './sampleData/example1.xls';
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' using \PhpOffice\PhpSpreadsheet\Reader\Xls<br />';
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Excel2003XML();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\SYLK();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Gnumeric();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\CSV();
$spreadsheet = $objReader->load($inputFileName);
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\Excel2003XML();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\SYLK();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\Gnumeric();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\CSV();
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -35,8 +35,8 @@ $inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$spreadsheet = $objReader->load($inputFileName);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -31,8 +31,8 @@ $inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);
echo 'File ',pathinfo($inputFileName, PATHINFO_BASENAME),' has been identified as an ',$inputFileType,' file<br />';
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' using IOFactory with the identified reader type<br />';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$spreadsheet = $objReader->load($inputFileName);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -33,10 +33,10 @@ $inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
echo 'Turning Formatting off for Load<br />';
$objReader->setReadDataOnly(true);
$spreadsheet = $objReader->load($inputFileName);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -33,10 +33,10 @@ $inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
echo 'Loading all WorkSheets<br />';
$objReader->setLoadAllSheets();
$spreadsheet = $objReader->load($inputFileName);
$reader->setLoadAllSheets();
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -34,10 +34,10 @@ $inputFileName = './sampleData/example1.xls';
$sheetname = 'Data Sheet #2';
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
echo 'Loading Sheet "',$sheetname,'" only<br />';
$objReader->setLoadSheetsOnly($sheetname);
$spreadsheet = $objReader->load($inputFileName);
$reader->setLoadSheetsOnly($sheetname);
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -34,10 +34,10 @@ $inputFileName = './sampleData/example1.xls';
$sheetnames = ['Data Sheet #1', 'Data Sheet #3'];
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
echo 'Loading Sheet',((count($sheetnames) == 1) ? '' : 's'),' "',implode('" and "', $sheetnames),'" only<br />';
$objReader->setLoadSheetsOnly($sheetnames);
$spreadsheet = $objReader->load($inputFileName);
$reader->setLoadSheetsOnly($sheetnames);
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -51,12 +51,12 @@ class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter
$filterSubset = new MyReadFilter();
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
echo 'Loading Sheet "',$sheetname,'" only<br />';
$objReader->setLoadSheetsOnly($sheetname);
$reader->setLoadSheetsOnly($sheetname);
echo 'Loading Sheet using filter<br />';
$objReader->setReadFilter($filterSubset);
$spreadsheet = $objReader->load($inputFileName);
$reader->setReadFilter($filterSubset);
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -63,12 +63,12 @@ class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter
$filterSubset = new MyReadFilter(9, 15, range('G', 'K'));
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
echo 'Loading Sheet "',$sheetname,'" only<br />';
$objReader->setLoadSheetsOnly($sheetname);
$reader->setLoadSheetsOnly($sheetname);
echo 'Loading Sheet using configurable filter<br />';
$objReader->setReadFilter($filterSubset);
$spreadsheet = $objReader->load($inputFileName);
$reader->setReadFilter($filterSubset);
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -59,7 +59,7 @@ class chunkReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter
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 = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
echo '<hr />';
@ -72,9 +72,9 @@ for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) {
/* 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);
$reader->setReadFilter($chunkFilter);
/* Load only the rows that match our filter from $inputFileName to a PHPExcel Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
// Do some processing here

View File

@ -59,7 +59,7 @@ class chunkReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter
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 = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
echo '<hr />';
@ -69,7 +69,7 @@ $chunkSize = 20;
$chunkFilter = new chunkReadFilter();
/* Tell the Reader that we want to use the Read Filter that we've Instantiated **/
$objReader->setReadFilter($chunkFilter);
$reader->setReadFilter($chunkFilter);
/* Loop to read our worksheet in "chunk size" blocks **/
for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) {
@ -77,7 +77,7 @@ for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) {
/* 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 **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
// Do some processing here

View File

@ -28,15 +28,15 @@ include 'PHPExcel/IOFactory.php';
$inputFileType = 'CSV';
$inputFileNames = ['./sampleData/example1.csv', './sampleData/example2.csv'];
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\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 />';
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
$spreadsheet->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, $spreadsheet);
$reader->setSheetIndex($sheet + 1);
$reader->loadIntoExisting($inputFileName, $spreadsheet);
$spreadsheet->getActiveSheet()->setTitle(pathinfo($inputFileName, PATHINFO_BASENAME));
}

View File

@ -50,7 +50,7 @@ class chunkReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter
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 = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
echo '<hr />';
@ -61,7 +61,7 @@ $chunkFilter = new chunkReadFilter();
/* Tell the Reader that we want to use the Read Filter that we've Instantiated **/
/* and that we want to store it in contiguous rows/columns **/
$objReader->setReadFilter($chunkFilter)
$reader->setReadFilter($chunkFilter)
->setContiguous(true);
/* Instantiate a new PHPExcel object manually **/
@ -77,9 +77,9 @@ for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) {
$chunkFilter->setRows($startRow, $chunkSize);
/* Increment the worksheet index pointer for the Reader **/
$objReader->setSheetIndex($sheet);
$reader->setSheetIndex($sheet);
/* Load only the rows that match our filter into a new worksheet in the PHPExcel Object **/
$objReader->loadIntoExisting($inputFileName, $spreadsheet);
$reader->loadIntoExisting($inputFileName, $spreadsheet);
/* Set the worksheet title (to reference the "sheet" of data that we've loaded) **/
/* and increment the sheet index as well **/
$spreadsheet->getActiveSheet()->setTitle('Country Data #' . (++$sheet));

View File

@ -25,10 +25,10 @@ include 'PHPExcel/IOFactory.php';
$inputFileType = 'CSV';
$inputFileName = './sampleData/example1.tsv';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\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");
$spreadsheet = $objReader->load($inputFileName);
$reader->setDelimiter("\t");
$spreadsheet = $reader->load($inputFileName);
$spreadsheet->getActiveSheet()->setTitle(pathinfo($inputFileName, PATHINFO_BASENAME));
echo '<hr />';

View File

@ -33,11 +33,11 @@ $inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/* Read the list of Worksheet Names from the Workbook file **/
echo 'Read the list of Worksheets in the WorkBook<br />';
$worksheetNames = $objReader->listWorksheetNames($inputFileName);
$worksheetNames = $reader->listWorksheetNames($inputFileName);
echo 'There are ',count($worksheetNames),' worksheet',((count($worksheetNames) == 1) ? '' : 's'),' in the workbook<br /><br />';
foreach ($worksheetNames as $worksheetName) {

View File

@ -34,8 +34,8 @@ $inputFileName = './sampleData/example1.xls';
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' information using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$worksheetNames = $objReader->listWorksheetNames($inputFileName);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$worksheetNames = $reader->listWorksheetNames($inputFileName);
echo '<h3>Worksheet Names</h3>';
echo '<ol>';

View File

@ -34,8 +34,8 @@ $inputFileName = './sampleData/example1.xls';
echo 'Loading file ',pathinfo($inputFileName, PATHINFO_BASENAME),' information using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$worksheetData = $objReader->listWorksheetInfo($inputFileName);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$worksheetData = $reader->listWorksheetInfo($inputFileName);
echo '<h3>Worksheet Information</h3>';
echo '<ol>';

View File

@ -29,9 +29,9 @@ $inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
/* Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/* Load $inputFileName to a PHPExcel Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -29,9 +29,9 @@ $inputFileType = 'Xlsx';
$inputFileName = './sampleData/example1.xlsx';
/* Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/* Load $inputFileName to a PHPExcel Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -29,9 +29,9 @@ $inputFileType = 'Xlsx';
$inputFileName = './sampleData/example1.xlsx';
/* Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/* Load $inputFileName to a PHPExcel Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -29,9 +29,9 @@ $inputFileType = 'Xls';
$inputFileName = './sampleData/example2.xls';
/* Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/* Load $inputFileName to a PHPExcel Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
echo '<hr />';

View File

@ -243,16 +243,16 @@ If you don't specify an index position as the second argument, then the new work
Sheets within the same workbook can be copied by creating a clone of the worksheet you wish to copy, and then using the addSheet() method to insert the clone into the workbook.
```php
$objClonedWorksheet = clone $spreadsheet->getSheetByName('Worksheet 1');
$objClonedWorksheet->setTitle('Copy of Worksheet 1')
$spreadsheet->addSheet($objClonedWorksheet);
$clonedWorksheet = clone $spreadsheet->getSheetByName('Worksheet 1');
$clonedWorksheet->setTitle('Copy of Worksheet 1')
$spreadsheet->addSheet($clonedWorksheet);
```
You can also copy worksheets from one workbook to another, though this is more complex as PhpSpreadsheet also has to replicate the styling between the two workbooks. The addExternalSheet() method is provided for this purpose.
```
$objClonedWorksheet = clone $spreadsheet1->getSheetByName('Worksheet 1');
$spreadsheet->addExternalSheet($objClonedWorksheet);
$clonedWorksheet = clone $spreadsheet1->getSheetByName('Worksheet 1');
$spreadsheet->addExternalSheet($clonedWorksheet);
```
In both cases, it is the developer's responsibility to ensure that worksheet names are not duplicated. PhpSpreadsheet will throw an exception if you attempt to copy worksheets that will result in a duplicate name.
@ -536,14 +536,14 @@ The easiest way to loop cells is by using iterators. Using iterators, one can us
Below is an example where we read all the values in a worksheet and display them in a table.
```php
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$objReader->setReadDataOnly(TRUE);
$spreadsheet = $objReader->load("test.xlsx");
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load("test.xlsx");
$objWorksheet = $spreadsheet->getActiveSheet();
$worksheet = $spreadsheet->getActiveSheet();
echo '<table>' . PHP_EOL;
foreach ($objWorksheet->getRowIterator() as $row) {
foreach ($worksheet->getRowIterator() as $row) {
echo '<tr>' . PHP_EOL;
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
@ -574,14 +574,14 @@ Note: In PhpSpreadsheet column index is 0-based while row index is 1-based. That
Below is an example where we read all the values in a worksheet and display them in a table.
```php
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$objReader->setReadDataOnly(TRUE);
$spreadsheet = $objReader->load("test.xlsx");
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load("test.xlsx");
$objWorksheet = $spreadsheet->getActiveSheet();
$worksheet = $spreadsheet->getActiveSheet();
// Get the highest row and column numbers referenced in the worksheet
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($highestColumn); // e.g. 5
echo '<table>' . "\n";
@ -589,7 +589,7 @@ for ($row = 1; $row <= $highestRow; ++$row) {
echo '<tr>' . PHP_EOL;
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
echo '<td>' .
$objWorksheet->getCellByColumnAndRow($col, $row)
$worksheet->getCellByColumnAndRow($col, $row)
->getValue() .
'</td>' . PHP_EOL;
}
@ -601,14 +601,14 @@ echo '</table>' . PHP_EOL;
Alternatively, you can take advantage of PHP's "Perl-style" character incrementors to loop through the cells by coordinate:
```php
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$objReader->setReadDataOnly(TRUE);
$spreadsheet = $objReader->load("test.xlsx");
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load("test.xlsx");
$objWorksheet = $spreadsheet->getActiveSheet();
$worksheet = $spreadsheet->getActiveSheet();
// Get the highest row number and column letter referenced in the worksheet
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
// Increment the highest column letter
$highestColumn++;
@ -617,7 +617,7 @@ for ($row = 1; $row <= $highestRow; ++$row) {
echo '<tr>' . PHP_EOL;
for ($col = 'A'; $col != $highestColumn; ++$col) {
echo '<td>' .
$objWorksheet->getCell($col . $row)
$worksheet->getCell($col . $row)
->getValue() .
'</td>' . PHP_EOL;
}
@ -685,16 +685,16 @@ A typical use of this feature is when you need to read files uploaded by your us
If you need to set some properties on the reader, (e.g. to only read data, see more about this later), then you may instead want to use this variant:
```php
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile("05featuredemo.xlsx");
$objReader->setReadDataOnly(true);
$objReader->load("05featuredemo.xlsx");
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile("05featuredemo.xlsx");
$reader->setReadDataOnly(true);
$reader->load("05featuredemo.xlsx");
```
You can create a \PhpOffice\PhpSpreadsheet\Reader\IReader instance using \PhpOffice\PhpSpreadsheet\IOFactory in explicit mode using the following code sample:
```php
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$spreadsheet = $objReader->load("05featuredemo.xlsx");
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$spreadsheet = $reader->load("05featuredemo.xlsx");
```
Note that automatic type resolving mode is slightly slower than explicit mode.
@ -704,8 +704,8 @@ Note that automatic type resolving mode is slightly slower than explicit mode.
You can create a PhpOffice\PhpSpreadsheet\Writer\IWriter instance using \PhpOffice\PhpSpreadsheet\IOFactory:
```php
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
$objWriter->save("05featuredemo.xlsx");
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
$writer->save("05featuredemo.xlsx");
```
## Excel 2007 (SpreadsheetML) file format
@ -719,8 +719,8 @@ Xlsx file format is the main file format of PhpSpreadsheet. It allows outputting
You can read an .xlsx file using the following code:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $objReader->load("05featuredemo.xlsx");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load("05featuredemo.xlsx");
```
#### Read data only
@ -728,9 +728,9 @@ $spreadsheet = $objReader->load("05featuredemo.xlsx");
You can set the option setReadDataOnly on the reader, to instruct the reader to ignore styling, data validation, … and just read cell data:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$objReader->setReadDataOnly(true);
$spreadsheet = $objReader->load("05featuredemo.xlsx");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load("05featuredemo.xlsx");
```
#### Read specific sheets only
@ -738,9 +738,9 @@ $spreadsheet = $objReader->load("05featuredemo.xlsx");
You can set the option setLoadSheetsOnly on the reader, to instruct the reader to only load the sheets with a given name:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$objReader->setLoadSheetsOnly( array("Sheet 1", "My special sheet") );
$spreadsheet = $objReader->load("05featuredemo.xlsx");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$reader->setLoadSheetsOnly( array("Sheet 1", "My special sheet") );
$spreadsheet = $reader->load("05featuredemo.xlsx");
```
#### Read specific cells only
@ -761,9 +761,9 @@ class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
}
}
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$objReader->setReadFilter( new MyReadFilter() );
$spreadsheet = $objReader->load("06largescale.xlsx");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$reader->setReadFilter( new MyReadFilter() );
$spreadsheet = $reader->load("06largescale.xlsx");
```
### \PhpOffice\PhpSpreadsheet\Writer\Xlsx
@ -773,8 +773,8 @@ $spreadsheet = $objReader->load("06largescale.xlsx");
You can write an .xlsx file using the following code:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$objWriter->save("05featuredemo.xlsx");
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save("05featuredemo.xlsx");
```
#### Formula pre-calculation
@ -782,9 +782,9 @@ $objWriter->save("05featuredemo.xlsx");
By default, this writer pre-calculates all formulas in the spreadsheet. This can be slow on large spreadsheets, and maybe even unwanted. You can however disable formula pre-calculation:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$objWriter->setPreCalculateFormulas(false);
$objWriter->save("05featuredemo.xlsx");
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->setPreCalculateFormulas(false);
$writer->save("05featuredemo.xlsx");
```
#### Office 2003 compatibility pack
@ -792,9 +792,9 @@ $objWriter->save("05featuredemo.xlsx");
Because of a bug in the Office2003 compatibility pack, there can be some small issues when opening Xlsx spreadsheets (mostly related to formula calculation). You can enable Office2003 compatibility with the following code:
```
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$objWriter->setOffice2003Compatibility(true);
$objWriter->save("05featuredemo.xlsx");
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->setOffice2003Compatibility(true);
$writer->save("05featuredemo.xlsx");
```
__Office2003 compatibility should only be used when needed__
@ -816,8 +816,8 @@ Please note that BIFF file format has some limits regarding to styling cells and
You can read an .xls file using the following code:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$spreadsheet = $objReader->load("05featuredemo.xls");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$spreadsheet = $reader->load("05featuredemo.xls");
```
#### Read data only
@ -825,9 +825,9 @@ $spreadsheet = $objReader->load("05featuredemo.xls");
You can set the option setReadDataOnly on the reader, to instruct the reader to ignore styling, data validation, … and just read cell data:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$objReader->setReadDataOnly(true);
$spreadsheet = $objReader->load("05featuredemo.xls");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load("05featuredemo.xls");
```
#### Read specific sheets only
@ -835,9 +835,9 @@ $spreadsheet = $objReader->load("05featuredemo.xls");
You can set the option setLoadSheetsOnly on the reader, to instruct the reader to only load the sheets with a given name:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$objReader->setLoadSheetsOnly( array("Sheet 1", "My special sheet") );
$spreadsheet = $objReader->load("05featuredemo.xls");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$reader->setLoadSheetsOnly( array("Sheet 1", "My special sheet") );
$spreadsheet = $reader->load("05featuredemo.xls");
```
#### Read specific cells only
@ -858,9 +858,9 @@ class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
}
}
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$objReader->setReadFilter( new MyReadFilter() );
$spreadsheet = $objReader->load("06largescale.xls");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$reader->setReadFilter( new MyReadFilter() );
$spreadsheet = $reader->load("06largescale.xls");
```
### \PhpOffice\PhpSpreadsheet\Writer\Xls
@ -870,8 +870,8 @@ $spreadsheet = $objReader->load("06largescale.xls");
You can write an .xls file using the following code:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet);
$objWriter->save("05featuredemo.xls");
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet);
$writer->save("05featuredemo.xls");
```
## Excel 2003 XML file format
@ -888,8 +888,8 @@ Please note that Excel 2003 XML format has some limits regarding to styling cell
You can read an Excel 2003 .xml file using the following code:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Excel2003XML();
$spreadsheet = $objReader->load("05featuredemo.xml");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Excel2003XML();
$spreadsheet = $reader->load("05featuredemo.xml");
```
#### Read specific cells only
@ -911,9 +911,9 @@ class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
}
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Excel2003XML();
$objReader->setReadFilter( new MyReadFilter() );
$spreadsheet = $objReader->load("06largescale.xml");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Excel2003XML();
$reader->setReadFilter( new MyReadFilter() );
$spreadsheet = $reader->load("06largescale.xml");
```
## Symbolic LinK (SYLK)
@ -930,8 +930,8 @@ Please note that SYLK file format has some limits regarding to styling cells and
You can read an .slk file using the following code:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\SYLK();
$spreadsheet = $objReader->load("05featuredemo.slk");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\SYLK();
$spreadsheet = $reader->load("05featuredemo.slk");
```
#### Read specific cells only
@ -953,9 +953,9 @@ class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
}
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\SYLK();
$objReader->setReadFilter( new MyReadFilter() );
$spreadsheet = $objReader->load("06largescale.slk");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\SYLK();
$reader->setReadFilter( new MyReadFilter() );
$spreadsheet = $reader->load("06largescale.slk");
```
## Open/Libre Office (.ods)
@ -969,8 +969,8 @@ Open Office or Libre Office .ods files are the standard file format for Open Off
You can read an .ods file using the following code:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
$spreadsheet = $objReader->load("05featuredemo.ods");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
$spreadsheet = $reader->load("05featuredemo.ods");
```
#### Read specific cells only
@ -992,9 +992,9 @@ class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
}
$objReader = new PhpOffice\PhpSpreadsheet\Reader\Ods();
$objReader->setReadFilter( new MyReadFilter() );
$spreadsheet = $objReader->load("06largescale.ods");
$reader = new PhpOffice\PhpSpreadsheet\Reader\Ods();
$reader->setReadFilter( new MyReadFilter() );
$spreadsheet = $reader->load("06largescale.ods");
```
## CSV (Comma Separated Values)
@ -1011,8 +1011,8 @@ Please note that CSV file format has some limits regarding to styling cells, num
You can read a .csv file using the following code:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\CSV();
$spreadsheet = $objReader->load("sample.csv");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\CSV();
$spreadsheet = $reader->load("sample.csv");
```
#### Setting CSV options
@ -1022,14 +1022,14 @@ Often, CSV files are not really “comma separated”, or use semicolon (;) as a
Note that \PhpOffice\PhpSpreadsheet\Reader\CSV by default assumes that the loaded CSV file is UTF-8 encoded. If you are reading CSV files that were created in Microsoft Office Excel the correct input encoding may rather be Windows-1252 (CP1252). Always make sure that the input encoding is set appropriately.
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\CSV();
$objReader->setInputEncoding('CP1252');
$objReader->setDelimiter(';');
$objReader->setEnclosure('');
$objReader->setLineEnding("\r\n");
$objReader->setSheetIndex(0);
$reader = new \PhpOffice\PhpSpreadsheet\Reader\CSV();
$reader->setInputEncoding('CP1252');
$reader->setDelimiter(';');
$reader->setEnclosure('');
$reader->setLineEnding("\r\n");
$reader->setSheetIndex(0);
$spreadsheet = $objReader->load("sample.csv");
$spreadsheet = $reader->load("sample.csv");
```
#### Read a specific worksheet
@ -1037,7 +1037,7 @@ $spreadsheet = $objReader->load("sample.csv");
CSV files can only contain one worksheet. Therefore, you can specify which sheet to read from CSV:
```php
$objReader->setSheetIndex(0);
$reader->setSheetIndex(0);
```
#### Read into existing spreadsheet
@ -1045,13 +1045,13 @@ $objReader->setSheetIndex(0);
When working with CSV files, it might occur that you want to import CSV data into an existing `Spreadsheet` object. The following code loads a CSV file into an existing $spreadsheet containing some sheets, and imports onto the 6th sheet:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\CSV();
$objReader->setDelimiter(';');
$objReader->setEnclosure('');
$objReader->setLineEnding("\r\n");
$objReader->setSheetIndex(5);
$reader = new \PhpOffice\PhpSpreadsheet\Reader\CSV();
$reader->setDelimiter(';');
$reader->setEnclosure('');
$reader->setLineEnding("\r\n");
$reader->setSheetIndex(5);
$objReader->loadIntoExisting("05featuredemo.csv", $spreadsheet);
$reader->loadIntoExisting("05featuredemo.csv", $spreadsheet);
```
### \PhpOffice\PhpSpreadsheet\Writer\CSV
@ -1061,8 +1061,8 @@ $objReader->loadIntoExisting("05featuredemo.csv", $spreadsheet);
You can write a .csv file using the following code:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\CSV($spreadsheet);
$objWriter->save("05featuredemo.csv");
$writer = new \PhpOffice\PhpSpreadsheet\Writer\CSV($spreadsheet);
$writer->save("05featuredemo.csv");
```
#### Setting CSV options
@ -1070,13 +1070,13 @@ $objWriter->save("05featuredemo.csv");
Often, CSV files are not really “comma separated”, or use semicolon (;) as a separator. You can instruct \PhpOffice\PhpSpreadsheet\Writer\CSV some options before writing a CSV file:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\CSV($spreadsheet);
$objWriter->setDelimiter(';');
$objWriter->setEnclosure('');
$objWriter->setLineEnding("\r\n");
$objWriter->setSheetIndex(0);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\CSV($spreadsheet);
$writer->setDelimiter(';');
$writer->setEnclosure('');
$writer->setLineEnding("\r\n");
$writer->setSheetIndex(0);
$objWriter->save("05featuredemo.csv");
$writer->save("05featuredemo.csv");
```
#### Write a specific worksheet
@ -1084,7 +1084,7 @@ $objWriter->save("05featuredemo.csv");
CSV files can only contain one worksheet. Therefore, you can specify which sheet to write to CSV:
```php
$objWriter->setSheetIndex(0);
$writer->setSheetIndex(0);
```
#### Formula pre-calculation
@ -1092,9 +1092,9 @@ $objWriter->setSheetIndex(0);
By default, this writer pre-calculates all formulas in the spreadsheet. This can be slow on large spreadsheets, and maybe even unwanted. You can however disable formula pre-calculation:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\CSV($spreadsheet);
$objWriter->setPreCalculateFormulas(false);
$objWriter->save("05featuredemo.csv");
$writer = new \PhpOffice\PhpSpreadsheet\Writer\CSV($spreadsheet);
$writer->setPreCalculateFormulas(false);
$writer->save("05featuredemo.csv");
```
#### Writing UTF-8 CSV files
@ -1102,9 +1102,9 @@ $objWriter->save("05featuredemo.csv");
A CSV file can be marked as UTF-8 by writing a BOM file header. This can be enabled by using the following code:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\CSV($spreadsheet);
$objWriter->setUseBOM(true);
$objWriter->save("05featuredemo.csv");
$writer = new \PhpOffice\PhpSpreadsheet\Writer\CSV($spreadsheet);
$writer->setUseBOM(true);
$writer->save("05featuredemo.csv");
```
#### Decimal and thousands separators
@ -1143,9 +1143,9 @@ Please note that HTML file format has some limits regarding to styling cells, nu
You can read an .html or .htm file using the following code:
```php
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\HTML();
$reader = new \PhpOffice\PhpSpreadsheet\Reader\HTML();
$spreadsheet = $objReader->load("05featuredemo.html");
$spreadsheet = $reader->load("05featuredemo.html");
```
__HTML limitations__
@ -1160,9 +1160,9 @@ Please note that \PhpOffice\PhpSpreadsheet\Writer\HTML only outputs the first wo
You can write a .htm file using the following code:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\HTML($spreadsheet);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\HTML($spreadsheet);
$objWriter->save("05featuredemo.htm");
$writer->save("05featuredemo.htm");
```
#### Write all worksheets
@ -1170,7 +1170,7 @@ $objWriter->save("05featuredemo.htm");
HTML files can contain one or more worksheets. If you want to write all sheets into a single HTML file, use the following code:
```php
$objWriter->writeAllSheets();
$writer->writeAllSheets();
```
#### Write a specific worksheet
@ -1178,7 +1178,7 @@ $objWriter->writeAllSheets();
HTML files can contain one or more worksheets. Therefore, you can specify which sheet to write to HTML:
```php
$objWriter->setSheetIndex(0);
$writer->setSheetIndex(0);
```
#### Setting the images root of the HTML file
@ -1197,7 +1197,7 @@ instead of
You can use the following code to achieve this result:
```php
$objWriter->setImagesRoot('http://www.example.com');
$writer->setImagesRoot('http://www.example.com');
```
#### Formula pre-calculation
@ -1205,10 +1205,10 @@ $objWriter->setImagesRoot('http://www.example.com');
By default, this writer pre-calculates all formulas in the spreadsheet. This can be slow on large spreadsheets, and maybe even unwanted. You can however disable formula pre-calculation:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\HTML($spreadsheet);
$objWriter->setPreCalculateFormulas(false);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\HTML($spreadsheet);
$writer->setPreCalculateFormulas(false);
$objWriter->save("05featuredemo.htm");
$writer->save("05featuredemo.htm");
```
#### Embedding generated HTML in a web page
@ -1226,8 +1226,8 @@ Here's an example which retrieves all parts independently and merges them into a
```php
<?php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\HTML($spreadsheet);
echo $objWriter->generateHTMLHeader();
$writer = new \PhpOffice\PhpSpreadsheet\Writer\HTML($spreadsheet);
echo $writer->generateHTMLHeader();
?>
<style>
@ -1239,15 +1239,15 @@ html {
}
<?php
echo $objWriter->generateStyles(false); // do not write <style> and </style>
echo $writer->generateStyles(false); // do not write <style> and </style>
?>
-->
</style>
<?php
echo $objWriter->generateSheetData();
echo $objWriter->generateHTMLFooter();
echo $writer->generateSheetData();
echo $writer->generateHTMLFooter();
?>
```
@ -1256,10 +1256,10 @@ echo $objWriter->generateHTMLFooter();
A HTML file can be marked as UTF-8 by writing a BOM file header. This can be enabled by using the following code:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\HTML($spreadsheet);
$objWriter->setUseBOM(true);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\HTML($spreadsheet);
$writer->setUseBOM(true);
$objWriter->save("05featuredemo.htm");
$writer->save("05featuredemo.htm");
```
#### Decimal and thousands separators
@ -1311,8 +1311,8 @@ if (!\PhpOffice\PhpSpreadsheet\Settings::setPdfRenderer(
Once you have identified the Renderer that you wish to use for PDF generation, you can write a .pdf file using the following code:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\PDF($spreadsheet);
$objWriter->save("05featuredemo.pdf");
$writer = new \PhpOffice\PhpSpreadsheet\Writer\PDF($spreadsheet);
$writer->save("05featuredemo.pdf");
```
Please note that \PhpOffice\PhpSpreadsheet\Writer\PDF only outputs the first worksheet by default.
@ -1322,7 +1322,7 @@ Please note that \PhpOffice\PhpSpreadsheet\Writer\PDF only outputs the first wor
PDF files can contain one or more worksheets. If you want to write all sheets into a single PDF file, use the following code:
```php
$objWriter->writeAllSheets();
$writer->writeAllSheets();
```
#### Write a specific worksheet
@ -1330,7 +1330,7 @@ $objWriter->writeAllSheets();
PDF files can contain one or more worksheets. Therefore, you can specify which sheet to write to PDF:
```php
$objWriter->setSheetIndex(0);
$writer->setSheetIndex(0);
```
#### Formula pre-calculation
@ -1338,10 +1338,10 @@ $objWriter->setSheetIndex(0);
By default, this writer pre-calculates all formulas in the spreadsheet. This can be slow on large spreadsheets, and maybe even unwanted. You can however disable formula pre-calculation:
```php
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\PDF($spreadsheet);
$objWriter->setPreCalculateFormulas(false);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\PDF($spreadsheet);
$writer->setPreCalculateFormulas(false);
$objWriter->save("05featuredemo.pdf");
$writer->save("05featuredemo.pdf");
```
#### Decimal and thousands separators
@ -1357,13 +1357,13 @@ Here is an example how to open a template file, fill in a couple of fields and s
```php
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('template.xlsx');
$objWorksheet = $spreadsheet->getActiveSheet();
$worksheet = $spreadsheet->getActiveSheet();
$objWorksheet->getCell('A1')->setValue('John');
$objWorksheet->getCell('A2')->setValue('Smith');
$worksheet->getCell('A1')->setValue('John');
$worksheet->getCell('A2')->setValue('Smith');
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$objWriter->save('write.xls');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('write.xls');
```
Notice that it is ok to load an xlsx file and generate an xls file.

View File

@ -39,15 +39,15 @@ If you know the file type of the spreadsheet file that you need to load, you can
$inputFileName = './sampleData/example1.xls';
/** Create a new Xls Reader **/
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Excel2003XML();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\SYLK();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Gnumeric();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\CSV();
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\Excel2003XML();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\SYLK();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\Gnumeric();
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\CSV();
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader02.php for a working example of this code.
@ -64,9 +64,9 @@ $inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader03.php for a working example of this code.
@ -78,9 +78,9 @@ $inputFileName = './sampleData/example1.xls';
/** Identify the type of $inputFileName **/
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);
/** Create a new Reader of the type that has been identified **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader04.php for a working example of this code.
@ -97,11 +97,11 @@ $inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader that we only want to load cell data **/
$objReader->setReadDataOnly(true);
$reader->setReadDataOnly(true);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader05.php for a working example of this code.
@ -129,11 +129,11 @@ $inputFileName = './sampleData/example1.xls';
$sheetname = 'Data Sheet #2';
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/
$objReader->setLoadSheetsOnly($sheetname);
$reader->setLoadSheetsOnly($sheetname);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader07.php for a working example of this code.
@ -145,11 +145,11 @@ $inputFileName = './sampleData/example1.xls';
$sheetnames = array('Data Sheet #1','Data Sheet #3');
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/
$objReader->setLoadSheetsOnly($sheetnames);
$reader->setLoadSheetsOnly($sheetnames);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader08.php for a working example of this code.
@ -160,11 +160,11 @@ $inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader to load all Worksheets **/
$objReader->setLoadAllSheets();
$reader->setLoadAllSheets();
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader06.php for a working example of this code.
@ -204,11 +204,11 @@ class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter
$filterSubset = new MyReadFilter();
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Tell the Reader that we want to use the Read Filter **/
$objReader->setReadFilter($filterSubset);
$reader->setReadFilter($filterSubset);
/** Load only the rows and columns that match our filter to Spreadsheet **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader09.php for a working example of this code.
@ -275,7 +275,7 @@ class chunkReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Define how many rows we want to read for each "chunk" **/
@ -284,14 +284,14 @@ $chunkSize = 2048;
$chunkFilter = new chunkReadFilter();
/** Tell the Reader that we want to use the Read Filter **/
$objReader->setReadFilter($chunkFilter);
$reader->setReadFilter($chunkFilter);
/** Loop to read our worksheet in "chunk size" blocks **/
for ($startRow = 2; $startRow <= 65536; $startRow += $chunkSize) {
/** Tell the Read Filter which rows we want this iteration **/
$chunkFilter->setRows($startRow,$chunkSize);
/** Load only the rows that match our filter **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
// Do some processing here
}
```
@ -307,7 +307,7 @@ CSV | YES | HTML | NO
### Combining Multiple Files into a Single Spreadsheet Object
While you can limit the number of worksheets that are read from a workbook file using the setLoadSheetsOnly() method, certain readers also allow you to combine several individual "sheets" from different files into a single `Spreadsheet` object, where each individual file is a single worksheet within that workbook. For each file that you read, you need to indicate which worksheet index it should be loaded into using the setSheetIndex() method of the $objReader, then use the loadIntoExisting() method rather than the load() method to actually read the file into that worksheet.
While you can limit the number of worksheets that are read from a workbook file using the setLoadSheetsOnly() method, certain readers also allow you to combine several individual "sheets" from different files into a single `Spreadsheet` object, where each individual file is a single worksheet within that workbook. For each file that you read, you need to indicate which worksheet index it should be loaded into using the setSheetIndex() method of the $reader, then use the loadIntoExisting() method rather than the load() method to actually read the file into that worksheet.
```php
$inputFileType = 'CSV';
@ -317,13 +317,13 @@ $inputFileNames = array('./sampleData/example1.csv',
);
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Extract the first named file from the array list **/
$inputFileName = array_shift($inputFileNames);
/** Load the initial file to the first worksheet in a `Spreadsheet` Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
/** Set the worksheet title (to the filename that we've loaded) **/
$spreadsheet->getActiveSheet()
->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
@ -332,9 +332,9 @@ $spreadsheet->getActiveSheet()
/** Loop through all the remaining files in the list **/
foreach($inputFileNames as $sheet => $inputFileName) {
/** Increment the worksheet index pointer for the Reader **/
$objReader->setSheetIndex($sheet+1);
$reader->setSheetIndex($sheet+1);
/** Load the current file into a new worksheet in Spreadsheet **/
$objReader->loadIntoExisting($inputFileName,$spreadsheet);
$reader->loadIntoExisting($inputFileName,$spreadsheet);
/** Set the worksheet title (to the filename that we've loaded) **/
$spreadsheet->getActiveSheet()
->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
@ -354,7 +354,7 @@ CSV | YES | HTML | NO
### Combining Read Filters with the setSheetIndex() method to split a large CSV file across multiple Worksheets
An Xls BIFF .xls file is limited to 65536 rows in a worksheet, while the Xlsx Microsoft Office Open XML SpreadsheetML .xlsx file is limited to 1,048,576 rows in a worksheet; but a CSV file is not limited other than by available disk space. This means that we wouldnt ordinarily be able to read all the rows from a very large CSV file that exceeded those limits, and save it as an Xls or Xlsx file. However, by using Read Filters to read the CSV file in “chunks” (using the chunkReadFilter Class that we defined in section REF _Ref275604563 \r \p 5.3 above), and the setSheetIndex() method of the $objReader, we can split the CSV file across several individual worksheets.
An Xls BIFF .xls file is limited to 65536 rows in a worksheet, while the Xlsx Microsoft Office Open XML SpreadsheetML .xlsx file is limited to 1,048,576 rows in a worksheet; but a CSV file is not limited other than by available disk space. This means that we wouldnt ordinarily be able to read all the rows from a very large CSV file that exceeded those limits, and save it as an Xls or Xlsx file. However, by using Read Filters to read the CSV file in “chunks” (using the chunkReadFilter Class that we defined in section REF _Ref275604563 \r \p 5.3 above), and the setSheetIndex() method of the $reader, we can split the CSV file across several individual worksheets.
```php
$inputFileType = 'CSV';
@ -363,7 +363,7 @@ $inputFileName = './sampleData/example2.csv';
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 = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Define how many rows we want to read for each "chunk" **/
@ -374,7 +374,7 @@ $chunkFilter = new chunkReadFilter();
/** Tell the Reader that we want to use the Read Filter **/
/** and that we want to store it in contiguous rows/columns **/
$objReader->setReadFilter($chunkFilter)
$reader->setReadFilter($chunkFilter)
->setContiguous(true);
/** Instantiate a new Spreadsheet object manually **/
@ -389,9 +389,9 @@ for ($startRow = 2; $startRow <= 1000000; $startRow += $chunkSize) {
$chunkFilter->setRows($startRow,$chunkSize);
/** Increment the worksheet index pointer for the Reader **/
$objReader->setSheetIndex($sheet);
$reader->setSheetIndex($sheet);
/** Load only the rows that match our filter into a new worksheet **/
$objReader->loadIntoExisting($inputFileName,$spreadsheet);
$reader->loadIntoExisting($inputFileName,$spreadsheet);
/** Set the worksheet title for the sheet that we've justloaded) **/
/** and increment the sheet index as well **/
$spreadsheet->getActiveSheet()->setTitle('Country Data #'.(++$sheet));
@ -421,13 +421,13 @@ The CSV loader defaults to loading a file where comma is used as the separator,
$inputFileType = 'CSV';
$inputFileName = './sampleData/example1.tsv';
/** Create a new Reader of the type defined in $inputFileType **/ $objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Create a new Reader of the type defined in $inputFileType **/ $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Set the delimiter to a TAB character **/
$objReader->setDelimiter("\t");
// $objReader->setDelimiter('|');
$reader->setDelimiter("\t");
// $reader->setDelimiter('|');
/** Load the file to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader15.php for a working example of this code.
@ -460,9 +460,9 @@ So using a Value Binder allows a great deal more flexibility in the loader logic
$inputFileType = 'CSV';
$inputFileName = './sampleData/example1.tsv';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$objReader->setDelimiter("\t");
$spreadsheet = $objReader->load($inputFileName);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setDelimiter("\t");
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader15.php for a working example of this code.
@ -489,11 +489,11 @@ $inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader that we only want to load cell data **/
$objReader->setReadDataOnly(true);
$reader->setReadDataOnly(true);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader05.php for a working example of this code.
@ -521,11 +521,11 @@ $inputFileName = './sampleData/example1.xls';
$sheetname = 'Data Sheet #2';
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/
$objReader->setLoadSheetsOnly($sheetname);
$reader->setLoadSheetsOnly($sheetname);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader07.php for a working example of this code.
@ -537,11 +537,11 @@ $inputFileName = './sampleData/example1.xls';
$sheetnames = array('Data Sheet #1','Data Sheet #3');
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/
$objReader->setLoadSheetsOnly($sheetnames);
$reader->setLoadSheetsOnly($sheetnames);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader08.php for a working example of this code.
@ -552,11 +552,11 @@ $inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader to load all Worksheets **/
$objReader->setLoadAllSheets();
$reader->setLoadAllSheets();
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader06.php for a working example of this code.
@ -596,11 +596,11 @@ class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter
$filterSubset = new MyReadFilter();
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Tell the Reader that we want to use the Read Filter **/
$objReader->setReadFilter($filterSubset);
$reader->setReadFilter($filterSubset);
/** Load only the rows and columns that match our filter to Spreadsheet **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader09.php for a working example of this code.
@ -667,7 +667,7 @@ class chunkReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Define how many rows we want to read for each "chunk" **/
@ -676,14 +676,14 @@ $chunkSize = 2048;
$chunkFilter = new chunkReadFilter();
/** Tell the Reader that we want to use the Read Filter **/
$objReader->setReadFilter($chunkFilter);
$reader->setReadFilter($chunkFilter);
/** Loop to read our worksheet in "chunk size" blocks **/
for ($startRow = 2; $startRow <= 65536; $startRow += $chunkSize) {
/** Tell the Read Filter which rows we want this iteration **/
$chunkFilter->setRows($startRow,$chunkSize);
/** Load only the rows that match our filter **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
// Do some processing here
}
```
@ -699,7 +699,7 @@ CSV | YES | HTML | NO
### Combining Multiple Files into a Single Spreadsheet Object
While you can limit the number of worksheets that are read from a workbook file using the setLoadSheetsOnly() method, certain readers also allow you to combine several individual "sheets" from different files into a single `Spreadsheet` object, where each individual file is a single worksheet within that workbook. For each file that you read, you need to indicate which worksheet index it should be loaded into using the setSheetIndex() method of the $objReader, then use the loadIntoExisting() method rather than the load() method to actually read the file into that worksheet.
While you can limit the number of worksheets that are read from a workbook file using the setLoadSheetsOnly() method, certain readers also allow you to combine several individual "sheets" from different files into a single `Spreadsheet` object, where each individual file is a single worksheet within that workbook. For each file that you read, you need to indicate which worksheet index it should be loaded into using the setSheetIndex() method of the $reader, then use the loadIntoExisting() method rather than the load() method to actually read the file into that worksheet.
```php
$inputFileType = 'CSV';
@ -709,13 +709,13 @@ $inputFileNames = array('./sampleData/example1.csv',
);
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Extract the first named file from the array list **/
$inputFileName = array_shift($inputFileNames);
/** Load the initial file to the first worksheet in a `Spreadsheet` Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
/** Set the worksheet title (to the filename that we've loaded) **/
$spreadsheet->getActiveSheet()
->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
@ -724,9 +724,9 @@ $spreadsheet->getActiveSheet()
/** Loop through all the remaining files in the list **/
foreach($inputFileNames as $sheet => $inputFileName) {
/** Increment the worksheet index pointer for the Reader **/
$objReader->setSheetIndex($sheet+1);
$reader->setSheetIndex($sheet+1);
/** Load the current file into a new worksheet in Spreadsheet **/
$objReader->loadIntoExisting($inputFileName,$spreadsheet);
$reader->loadIntoExisting($inputFileName,$spreadsheet);
/** Set the worksheet title (to the filename that we've loaded) **/
$spreadsheet->getActiveSheet()
->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
@ -746,7 +746,7 @@ CSV | YES | HTML | NO
### Combining Read Filters with the setSheetIndex() method to split a large CSV file across multiple Worksheets
An Xls BIFF .xls file is limited to 65536 rows in a worksheet, while the Xlsx Microsoft Office Open XML SpreadsheetML .xlsx file is limited to 1,048,576 rows in a worksheet; but a CSV file is not limited other than by available disk space. This means that we wouldnt ordinarily be able to read all the rows from a very large CSV file that exceeded those limits, and save it as an Xls or Xlsx file. However, by using Read Filters to read the CSV file in “chunks” (using the chunkReadFilter Class that we defined in section REF _Ref275604563 \r \p 5.3 above), and the setSheetIndex() method of the $objReader, we can split the CSV file across several individual worksheets.
An Xls BIFF .xls file is limited to 65536 rows in a worksheet, while the Xlsx Microsoft Office Open XML SpreadsheetML .xlsx file is limited to 1,048,576 rows in a worksheet; but a CSV file is not limited other than by available disk space. This means that we wouldnt ordinarily be able to read all the rows from a very large CSV file that exceeded those limits, and save it as an Xls or Xlsx file. However, by using Read Filters to read the CSV file in “chunks” (using the chunkReadFilter Class that we defined in section REF _Ref275604563 \r \p 5.3 above), and the setSheetIndex() method of the $reader, we can split the CSV file across several individual worksheets.
```php
$inputFileType = 'CSV';
@ -755,7 +755,7 @@ $inputFileName = './sampleData/example2.csv';
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 = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Define how many rows we want to read for each "chunk" **/
@ -766,7 +766,7 @@ $chunkFilter = new chunkReadFilter();
/** Tell the Reader that we want to use the Read Filter **/
/** and that we want to store it in contiguous rows/columns **/
$objReader->setReadFilter($chunkFilter)
$reader->setReadFilter($chunkFilter)
->setContiguous(true);
/** Instantiate a new Spreadsheet object manually **/
@ -781,9 +781,9 @@ for ($startRow = 2; $startRow <= 1000000; $startRow += $chunkSize) {
$chunkFilter->setRows($startRow,$chunkSize);
/** Increment the worksheet index pointer for the Reader **/
$objReader->setSheetIndex($sheet);
$reader->setSheetIndex($sheet);
/** Load only the rows that match our filter into a new worksheet **/
$objReader->loadIntoExisting($inputFileName,$spreadsheet);
$reader->loadIntoExisting($inputFileName,$spreadsheet);
/** Set the worksheet title for the sheet that we've justloaded) **/
/** and increment the sheet index as well **/
$spreadsheet->getActiveSheet()->setTitle('Country Data #'.(++$sheet));
@ -813,13 +813,13 @@ The CSV loader defaults to loading a file where comma is used as the separator,
$inputFileType = 'CSV';
$inputFileName = './sampleData/example1.tsv';
/** Create a new Reader of the type defined in $inputFileType **/ $objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Create a new Reader of the type defined in $inputFileType **/ $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Set the delimiter to a TAB character **/
$objReader->setDelimiter("\t");
// $objReader->setDelimiter('|');
$reader->setDelimiter("\t");
// $reader->setDelimiter('|');
/** Load the file to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader15.php for a working example of this code.
@ -852,9 +852,9 @@ So using a Value Binder allows a great deal more flexibility in the loader logic
$inputFileType = 'CSV';
$inputFileName = './sampleData/example1.tsv';
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$objReader->setDelimiter("\t");
$spreadsheet = $objReader->load($inputFileName);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setDelimiter("\t");
$spreadsheet = $reader->load($inputFileName);
```
> See Examples/Reader/exampleReader15.php for a working example of this code.
@ -897,9 +897,9 @@ You can retrieve a list of worksheet names contained in a file without loading t
The `listWorksheetNames()` method returns a simple array listing each worksheet name within the workbook:
```php
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$worksheetNames = $objReader->listWorksheetNames($inputFileName);
$worksheetNames = $reader->listWorksheetNames($inputFileName);
echo '<h3>Worksheet Names</h3>';
echo '<ol>';
@ -915,9 +915,9 @@ echo '</ol>';
The `listWorksheetInfo()` method returns a nested array, with each entry listing the name and dimensions for a worksheet:
```php
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$worksheetData = $objReader->listWorksheetInfo($inputFileName);
$worksheetData = $reader->listWorksheetInfo($inputFileName);
echo '<h3>Worksheet Information</h3>';
echo '<ol>';

View File

@ -358,11 +358,11 @@ Code | Meaning
[^print-footer-image-footnote]: z
```php
$objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing();
$objDrawing->setName('PhpSpreadsheet logo');
$objDrawing->setPath('./images/PhpSpreadsheet_logo.png');
$objDrawing->setHeight(36);
$spreadsheet->getActiveSheet()->getHeaderFooter()->addImage($objDrawing, \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter::IMAGE_HEADER_LEFT);
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing();
$drawing->setName('PhpSpreadsheet logo');
$drawing->setPath('./images/PhpSpreadsheet_logo.png');
$drawing->setHeight(36);
$spreadsheet->getActiveSheet()->getHeaderFooter()->addImage($drawing, \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter::IMAGE_HEADER_LEFT);
```
__Tip__
@ -371,12 +371,12 @@ The above table of codes may seem overwhelming first time you are trying to figu
```php
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('test.xlsx');
$objWorksheet = $spreadsheet->getActiveSheet();
$worksheet = $spreadsheet->getActiveSheet();
var_dump($objWorksheet->getHeaderFooter()->getOddFooter());
var_dump($objWorksheet->getHeaderFooter()->getEvenFooter());
var_dump($objWorksheet->getHeaderFooter()->getOddHeader());
var_dump($objWorksheet->getHeaderFooter()->getEvenHeader());
var_dump($worksheet->getHeaderFooter()->getOddFooter());
var_dump($worksheet->getHeaderFooter()->getEvenFooter());
var_dump($worksheet->getHeaderFooter()->getOddHeader());
var_dump($worksheet->getHeaderFooter()->getEvenHeader());
```
That reveals the codes for the even/odd header and footer. Experienced users may find it easier to rename test.xlsx to test.zip, unzip it, and inspect directly the contents of the relevant xl/worksheets/sheetX.xml to find the codes for header/footer.
@ -542,8 +542,8 @@ The rules for composing a number format code in Excel can be rather complicated.
The readers shipped with PhpSpreadsheet come to the rescue. Load your template workbook using e.g. Xlsx reader to reveal the number format code. Example how read a number format code for cell A1:
```php
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$spreadsheet = $objReader->load('template.xlsx');
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load('template.xlsx');
var_dump($spreadsheet->getActiveSheet()->getStyle('A1')->getNumberFormat()->getFormatCode());
```
@ -588,7 +588,7 @@ $styleArray = array(
),
);
$objWorksheet->getStyle('B2:G8')->applyFromArray($styleArray);
$worksheet->getStyle('B2:G8')->applyFromArray($styleArray);
```
In Microsoft Office Excel, the above operation would correspond to selecting the cells B2:G8, launching the style dialog, choosing a thick red border, and clicking on the "Outline" border component.
@ -722,23 +722,23 @@ A cell can be formatted conditionally, based on a specific rule. For example, on
One can set a conditional style ruleset to a cell using the following code:
```php
$objConditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$objConditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS);
$objConditional1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_LESSTHAN);
$objConditional1->addCondition('0');
$objConditional1->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
$objConditional1->getStyle()->getFont()->setBold(true);
$conditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS);
$conditional1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_LESSTHAN);
$conditional1->addCondition('0');
$conditional1->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
$conditional1->getStyle()->getFont()->setBold(true);
$objConditional2 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$objConditional2->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS);
$objConditional2->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_GREATERTHANOREQUAL);
$objConditional2->addCondition('0');
$objConditional2->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
$objConditional2->getStyle()->getFont()->setBold(true);
$conditional2 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional2->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS);
$conditional2->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_GREATERTHANOREQUAL);
$conditional2->addCondition('0');
$conditional2->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
$conditional2->getStyle()->getFont()->setBold(true);
$conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('B2')->getConditionalStyles();
array_push($conditionalStyles, $objConditional1);
array_push($conditionalStyles, $objConditional2);
array_push($conditionalStyles, $conditional1);
array_push($conditionalStyles, $conditional2);
$spreadsheet->getActiveSheet()->getStyle('B2')->setConditionalStyles($conditionalStyles);
```
@ -761,10 +761,10 @@ To add a comment to a cell, use the following code. The example below adds a com
$spreadsheet->getActiveSheet()
->getComment('E11')
->setAuthor('Mark Baker');
$objCommentRichText = $spreadsheet->getActiveSheet()
$commentRichText = $spreadsheet->getActiveSheet()
->getComment('E11')
->getText()->createTextRun('PhpSpreadsheet:');
$objCommentRichText->getFont()->setBold(true);
$commentRichText->getFont()->setBold(true);
$spreadsheet->getActiveSheet()
->getComment('E11')
->getText()->createTextRun("\r\n");
@ -834,47 +834,47 @@ Data validation is a powerful feature of Xlsx. It allows to specify an input fil
The following piece of code only allows numbers between 10 and 20 to be entered in cell B3:
```php
$objValidation = $spreadsheet->getActiveSheet()->getCell('B3')
$validation = $spreadsheet->getActiveSheet()->getCell('B3')
->getDataValidation();
$objValidation->setType( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::TYPE_WHOLE );
$objValidation->setErrorStyle( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::STYLE_STOP );
$objValidation->setAllowBlank(true);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setErrorTitle('Input error');
$objValidation->setError('Number is not allowed!');
$objValidation->setPromptTitle('Allowed input');
$objValidation->setPrompt('Only numbers between 10 and 20 are allowed.');
$objValidation->setFormula1(10);
$objValidation->setFormula2(20);
$validation->setType( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::TYPE_WHOLE );
$validation->setErrorStyle( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::STYLE_STOP );
$validation->setAllowBlank(true);
$validation->setShowInputMessage(true);
$validation->setShowErrorMessage(true);
$validation->setErrorTitle('Input error');
$validation->setError('Number is not allowed!');
$validation->setPromptTitle('Allowed input');
$validation->setPrompt('Only numbers between 10 and 20 are allowed.');
$validation->setFormula1(10);
$validation->setFormula2(20);
```
The following piece of code only allows an item picked from a list of data to be entered in cell B3:
```php
$objValidation = $spreadsheet->getActiveSheet()->getCell('B5')
$validation = $spreadsheet->getActiveSheet()->getCell('B5')
->getDataValidation();
$objValidation->setType( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::TYPE_LIST );
$objValidation->setErrorStyle( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::STYLE_INFORMATION );
$objValidation->setAllowBlank(false);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setShowDropDown(true);
$objValidation->setErrorTitle('Input error');
$objValidation->setError('Value is not in list.');
$objValidation->setPromptTitle('Pick from list');
$objValidation->setPrompt('Please pick a value from the drop-down list.');
$objValidation->setFormula1('"Item A,Item B,Item C"');
$validation->setType( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::TYPE_LIST );
$validation->setErrorStyle( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::STYLE_INFORMATION );
$validation->setAllowBlank(false);
$validation->setShowInputMessage(true);
$validation->setShowErrorMessage(true);
$validation->setShowDropDown(true);
$validation->setErrorTitle('Input error');
$validation->setError('Value is not in list.');
$validation->setPromptTitle('Pick from list');
$validation->setPrompt('Please pick a value from the drop-down list.');
$validation->setFormula1('"Item A,Item B,Item C"');
```
When using a data validation list like above, make sure you put the list between " and " and that you split the items with a comma (,).
It is important to remember that any string participating in an Excel formula is allowed to be maximum 255 characters (not bytes). This sets a limit on how many items you can have in the string "Item A,Item B,Item C". Therefore it is normally a better idea to type the item values directly in some cell range, say A1:A3, and instead use, say, $objValidation->setFormula1('Sheet!$A$1:$A$3');. Another benefit is that the item values themselves can contain the comma "," character itself.
It is important to remember that any string participating in an Excel formula is allowed to be maximum 255 characters (not bytes). This sets a limit on how many items you can have in the string "Item A,Item B,Item C". Therefore it is normally a better idea to type the item values directly in some cell range, say A1:A3, and instead use, say, $validation->setFormula1('Sheet!$A$1:$A$3');. Another benefit is that the item values themselves can contain the comma "," character itself.
If you need data validation on multiple cells, one can clone the ruleset:
```php
$spreadsheet->getActiveSheet()->getCell('B8')->setDataValidation(clone $objValidation);
$spreadsheet->getActiveSheet()->getCell('B8')->setDataValidation(clone $validation);
```
## Setting a column's width
@ -1011,30 +1011,30 @@ $spreadsheet->getActiveSheet()->insertNewRowBefore(7, 2);
A drawing is always represented as a separate object, which can be added to a worksheet. Therefore, you must first instantiate a new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing, and assign its properties a meaningful value:
```php
$objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath('./images/officelogo.jpg');
$objDrawing->setHeight(36);
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Logo');
$drawing->setDescription('Logo');
$drawing->setPath('./images/officelogo.jpg');
$drawing->setHeight(36);
```
To add the above drawing to the worksheet, use the following snippet of code. PhpSpreadsheet creates the link between the drawing and the worksheet:
```php
$objDrawing->setWorksheet($spreadsheet->getActiveSheet());
$drawing->setWorksheet($spreadsheet->getActiveSheet());
```
You can set numerous properties on a drawing, here are some examples:
```php
$objDrawing->setName('Paid');
$objDrawing->setDescription('Paid');
$objDrawing->setPath('./images/paid.png');
$objDrawing->setCoordinates('B15');
$objDrawing->setOffsetX(110);
$objDrawing->setRotation(25);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(45);
$drawing->setName('Paid');
$drawing->setDescription('Paid');
$drawing->setPath('./images/paid.png');
$drawing->setCoordinates('B15');
$drawing->setOffsetX(110);
$drawing->setRotation(25);
$drawing->getShadow()->setVisible(true);
$drawing->getShadow()->setDirection(45);
```
You can also add images created using GD functions without needing to save them to disk first as In-Memory drawings.
@ -1046,17 +1046,17 @@ $textColor = imagecolorallocate($gdImage, 255, 255, 255);
imagestring($gdImage, 1, 5, 5, 'Created with PhpSpreadsheet', $textColor);
// Add the In-Memory image to a worksheet
$objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing();
$objDrawing->setName('In-Memory image 1');
$objDrawing->setDescription('In-Memory image 1');
$objDrawing->setCoordinates('A1');
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing();
$drawing->setName('In-Memory image 1');
$drawing->setDescription('In-Memory image 1');
$drawing->setCoordinates('A1');
$drawing->setImageResource($gdImage);
$drawing->setRenderingFunction(
\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::RENDERING_JPEG
);
$objDrawing->setMimeType(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(36);
$objDrawing->setWorksheet($spreadsheet->getActiveSheet());
$drawing->setMimeType(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_DEFAULT);
$drawing->setHeight(36);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
```
## Reading Images from a worksheet
@ -1108,14 +1108,14 @@ Adding rich text to a cell can be done using \PhpOffice\PhpSpreadsheet\RichText
> This invoice is *__payable within thirty days after the end of the month__* unless specified otherwise on the invoice.
```php
$objRichText = new \PhpOffice\PhpSpreadsheet\RichText();
$objRichText->createText('This invoice is ');
$objPayable = $objRichText->createTextRun('payable within thirty days after the end of the month');
$objPayable->getFont()->setBold(true);
$objPayable->getFont()->setItalic(true);
$objPayable->getFont()->setColor( new \PhpOffice\PhpSpreadsheet\Style\Color( \PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKGREEN ) );
$objRichText->createText(', unless specified otherwise on the invoice.');
$spreadsheet->getActiveSheet()->getCell('A18')->setValue($objRichText);
$richText = new \PhpOffice\PhpSpreadsheet\RichText();
$richText->createText('This invoice is ');
$payable = $richText->createTextRun('payable within thirty days after the end of the month');
$payable->getFont()->setBold(true);
$payable->getFont()->setItalic(true);
$payable->getFont()->setColor( new \PhpOffice\PhpSpreadsheet\Style\Color( \PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKGREEN ) );
$richText->createText(', unless specified otherwise on the invoice.');
$spreadsheet->getActiveSheet()->getCell('A18')->setValue($richText);
```
## Define a named range
@ -1162,8 +1162,8 @@ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetm
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$objWriter->save('php://output');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
```
Example of a script redirecting an Xls file to the client's browser:
@ -1176,8 +1176,8 @@ header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="myfile.xls"');
header('Cache-Control: max-age=0');
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$objWriter->save('php://output');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output');
```
**Caution:**
@ -1214,14 +1214,14 @@ $textColor = imagecolorallocate($gdImage, 255, 255, 255);
imagestring($gdImage, 1, 5, 5, 'Created with PhpSpreadsheet', $textColor);
// Add a drawing to the worksheet
$objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing();
$objDrawing->setName('Sample image');
$objDrawing->setDescription('Sample image');
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(36);
$objDrawing->setWorksheet($spreadsheet->getActiveSheet());
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing();
$drawing->setName('Sample image');
$drawing->setDescription('Sample image');
$drawing->setImageResource($gdImage);
$drawing->setRenderingFunction(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::RENDERING_JPEG);
$drawing->setMimeType(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_DEFAULT);
$drawing->setHeight(36);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
```
## Setting worksheet zoom level
@ -1239,7 +1239,7 @@ Note that zoom level should be in range 10 - 400.
Sometimes you want to set a color for sheet tab. For example you can have a red sheet tab:
```php
$objWorksheet->getTabColor()->setRGB('FF0000');
$worksheet->getTabColor()->setRGB('FF0000');
```
## Creating worksheets in a workbook
@ -1247,8 +1247,8 @@ $objWorksheet->getTabColor()->setRGB('FF0000');
If you need to create more worksheets in the workbook, here is how:
```php
$objWorksheet1 = $spreadsheet->createSheet();
$objWorksheet1->setTitle('Another sheet');
$worksheet1 = $spreadsheet->createSheet();
$worksheet1->setTitle('Another sheet');
```
Think of createSheet() as the "Insert sheet" button in Excel. When you hit that button a new sheet is appended to the existing collection of worksheets in the workbook.