PhpSpreadsheet 块过滤器读取返回空行
Posted
技术标签:
【中文标题】PhpSpreadsheet 块过滤器读取返回空行【英文标题】:PhpSpreadsheet chunk filter read returning empty rows 【发布时间】:2018-10-10 00:18:57 【问题描述】:我正在尝试按块读取 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);
【问题讨论】:
【参考方案1】:PHPSpreadsheet 卡盘阅读器仅返回当前页面值并基于 NULL,
请检查以下可以解决您问题的代码。
$startRow = 1;
$arrStrHeadings = [];
$chunkFilter = new ChunkReadFilter();
$objCsvReader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$objCsvReader->setReadDataOnly(true);
$objCsvReader->setReadFilter($chunkFilter);
while (true)
$chunkFilter->setRows($startRow, self::CHUNK_LIMIT);
$objWorksheet = $objCsvReader->load($arrMixSource['file'])->getActiveSheet();
$pRange = 'A' . $startRow . ':' . $objWorksheet->getHighestColumn() . $objWorksheet->getHighestRow();
$arrStrSheetData = $objWorksheet->rangeToArray( $pRange, $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false);
if (0 == count($arrStrSheetData))
break;
if( 1 == $startRow && empty($arrStrHeadings))
$arrStrHeadings = array_map('strtolower', array_shift($arrStrSheetData));
foreach ($arrStrSheetData as $arrStrSheetDatum)
$arrStrSheetDatum = array_combine($arrStrHeadings, $arrStrSheetDatum);
// Do oprations...
$startRow += self::CHUNK_LIMIT;
【讨论】:
以上是关于PhpSpreadsheet 块过滤器读取返回空行的主要内容,如果未能解决你的问题,请参考以下文章
phpoffice/phpspreadsheet读取Excel内的图片