我正在尝试按块读取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);
}
答案 0 :(得分:0)
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;
}