4

我正在尝试按块读取 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);
   }
4

1 回答 1

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;
}
于 2018-08-16T05:20:53.470 回答