我正在使用 PhpSpreadsheet 轻松读取 xls 文档并在进行一些计算后插入到数据库中。我成功地使用了文档中的示例,但我发现它太复杂了,我确定我错过了一些东西,而且可以更轻松地完成。

$worksheet = $this->getWorksheet("file.xls");
foreach ($worksheet->getRowIterator() as $row) {
  $cellIterator = $row->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(FALSE);
  foreach ($cellIterator as $key => $cell) {
    $cellValue = $cell->getValue();

    if($key == 'A')
      $field1 = $cellValue;
    if($key == 'B') {
      $dateTime = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cellValue);
      $date = $dateTime->format("Y-m-d");
    }
    if($key == 'C')
      $field2 = $cellValue;
    if($key == 'D')
      $field3 = $cellValue;
    if($key == 'E')
      $field4 = $cellValue;
  }
}

我本来希望像 $row->getCell("A")->getValue() 这样的东西可用。

所以......我错过了什么吗?

最佳答案

希望这可以帮助

$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); // e.g. 5
for ($row = 1; $row <= $highestRow; ++$row) {
    for ($col = 1; $col <= $highestColumnIndex; ++$col) {
        $value = $worksheet->getCellByColumnAndRow($col, $row)->getValue();
    }
}

关于phpexcel - PhpSpreadsheet - 在不迭代每个单元格的情况下获取行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49596810/

10-13 02:03