我正在尝试按块读取 XLSX 文件。但是有些块在输出时是空的var_dump
。我正在使用 PHP7.2 和 Laravel 5.5 框架。
这就是我到目前为止所尝试的:
筛选
class ChunkReadFilter implements 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 configured rows
// if (($row == 1) || ($row >= $this->startRow && $row < $this->endRow)) {
if (($row >= $this->startRow && $row < $this->endRow)) {
if (in_array($column, range('A', 'L'))) {
return true;
}
}
return false;
}
}
代码
$inputFileType = 'Xlsx';
$inputFileName = $path;
$reader = IOFactory::createReader($inputFileType);
$chunkSize = 100;
$chunkFilter = new ChunkReadFilter();
for ($startRow = 1; $startRow <= 800; $startRow += $chunkSize) {
//Tell the Read Filter which rows we want this iteration
$chunkFilter->setRows($startRow,$chunkSize);
//Tell the Reader that we want to use the Read Filter
$reader->setReadFilter($chunkFilter);
//Load only the rows that match our filter
$spreadsheet = $reader->load($inputFileName);
//Do some processing here
$sheetData = $spreadsheet->getActiveSheet()
->toArray(null, true, true, true);
var_dump($sheetData);
}