PhpSpreadsheet/samples/24_Readfilter.php
Adrien Crivelli 2922a13764
Reorganize code samples
This introduce a helper class that should be used to log things,
avoiding a lot of boilerplate code.

Also all output are made in /tmp folder instead of beside the script
itself. This is because there is a high chance that the folder containing
the script is not writtable by webserver. So using the /tmp folder
makes it more likely to works in a variety of setup.
2016-09-01 01:17:13 +09:00

39 lines
1.1 KiB
PHP

<?php
namespace PhpSpreadsheet;
require __DIR__ . '/Header.php';
// Write temporary file
$largeSpreadsheet = require __DIR__ . '/templates/largeSpreadsheet.php';
$writer = new \PhpSpreadsheet\Writer\Excel2007($largeSpreadsheet);
$filename = $helper->getTemporaryFilename();
$callStartTime = microtime(true);
$writer->save($filename);
$helper->logWrite($writer, $filename, $callStartTime);
class MyReadFilter implements \PhpSpreadsheet\Reader\IReadFilter
{
public function readCell($column, $row, $worksheetName = '')
{
// Read title row and rows 20 - 30
if ($row == 1 || ($row >= 20 && $row <= 30)) {
return true;
}
return false;
}
}
$helper->log('Load from Excel2007 file');
$reader = \PhpSpreadsheet\IOFactory::createReader('Excel2007');
$reader->setReadFilter(new MyReadFilter());
$callStartTime = microtime(true);
$spreadsheet = $reader->load($filename);
$helper->logRead('Excel2007', $filename, $callStartTime);
$helper->log('Remove unnecessary rows');
$spreadsheet->getActiveSheet()->removeRow(2, 18);
// Save
$helper->write($spreadsheet, __FILE__);