PhpSpreadsheet - 让行不反复对每一个电池

问题描述 投票:4回答:2

我使用PhpSpreadsheet轻松地从一个XLS文档阅读和一些计算后插入到数据库中。我成功地使用从实例文档,但我觉得它复杂的sooo我敢肯定,我错过了什么,它可以更容易地完成。

$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()可用。

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

phpexcel phpspreadsheet
2个回答
5
投票

希望这可以帮助

$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();
    }
}    

0
投票

查看由列和行得到的值,而不是直接测试键docs

从这个例子:

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();

希望帮助

© www.soinside.com 2019 - 2024. All rights reserved.