使用PHPExcel,我遇到了getHighestRow()函数的问题。如果电子表格的格式超出最后一个数据条目,则最高行不会反映数据输入,但会修改单元格,包括格式化。
如何获得有用行数,仅表示存在非空数据的行?
到目前为止,我使用以下代码作为替代,但我不满意加载整个第一列:
$file = $fileForm->get('file')->getData();
$path = $file->getRealPath();
$phpExcel = $this->get('phpexcel')->createPHPExcelObject($path);
$worksheet = $phpExcel->getSheet();
$firstColumn = $worksheet->rangeToArray("A1:A" . $worksheet->getHighestRow(), null, false, false);
$firstColumn = array_map(function(Array $lines) {
return array_values($lines)[0];
}, $firstColumn);
$highestRow = count(array_filter($firstColumn));
unset($firstColumn);
答案 0 :(得分:0)
这就是为什么还有一个
$worksheet->getHighestDataRow()
方法
答案 1 :(得分:0)
$spreadsheet = IOFactory::load($file_path);
$sheet = $spreadsheet->getActiveSheet();
$row_index = $sheet->getHighestDataRow();
// Avoid to rewrite the first line.
// We need to add the checking because when we hav an empty file
// the $row_index will be 1. It happened beause the Exel index starts
// from 1. It can't be less than 1.
// So when there 0 lines we have row_index=1
// and whne we have 1 line we also have row_index=1.
if ($row_index == 1) {
$cell_value = $sheet->getCell("A$row_index")->getValue();
if ($cell_value !== NULL && $cell_value != '') {
$row_index++;
}
}