我写了一个导入xlsx文件的API。很好。
在下一个函数中,我编写了一个逻辑,该逻辑需要提取该文件,但我不知道是什么导致了此错误:
在null上调用成员函数getRowIterator()
我的第一个电话
public function importXlsx($fileLocation)
{
$reader = IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($fileLocation);
$this->importSheet($spreadsheet, 'One', function(User $user) {
$user->setType();
return $user;
});
$this->importSheet($spreadsheet, 'Two', function(User $user) {
$user->setType2();
return $user;
});
die('done');
}
我的第二通电话:
private function importSheet($spreadsheet, $sheetName, callable $additionalManipulation = null)
{
$worksheet = $spreadsheet->getSheetByName($sheetName);
$columnMappings = [
'First Name' => 'firstName',
'Surname' => 'lastName',
'Email' => 'email',
];
$headerColumns = null;
$data = [];
foreach ($worksheet->getRowIterator() as $index => $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE);
$rowData = [];
foreach ($cellIterator as $cell) {
$rowData[]= $cell->getValue();
}
if($index == 1) {
$headerColumns = $rowData;
} else {
$rowData = array_combine($headerColumns, $rowData);
$data[$index] =$rowData;
}
}