[英]phpoffice/phpspreadsheet - How to exclude no value cells from getHighestRow()
[英]How to read value from merged cells in PhpSpreadsheet
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load($_FILES['mapping_file']['tmp_name']);
$sheetData = $spreadsheet->getActiveSheet()->toArray();
這是我使用PhpSpreadsheet從php讀取excel數據的代碼,返回是數組。 但是合並單元格時,它只返回合並單元格的單個值,其他值為null。
// loop to fill empty value from merged cells //
for($i=0; $i<count($DuplicatedSheetData);$i++){
// explode merge cells range //
$CellIndex = explode(":", $DuplicatedSheetData[$i]);
// get main cell with value, ex N25:N27 , the value only stored in N25 //
$CellValue = $spreadsheet->getActiveSheet()->getCell($CellIndex[0])->getValue();
// starting index //
$StartIndex = (int) substr($CellIndex[0],1,2);
// ending index //
$EndIndex = (int) substr($CellIndex[1],1,2);
// column name example "A" ,"B" //
$ColumnName = substr($CellIndex[0],0,1);
// loop to copy the value from main cell, to other cell //
for($j = $StartIndex;$j <= $EndIndex; $j++){
$spreadsheet->getActiveSheet()->setCellValue($ColumnName.$j , $CellValue);
}
}
// map to array and remove empty value //
$sheetData = $spreadsheet->getActiveSheet()->toArray();
$sheetData = array_values(array_map('array_filter', $sheetData));
基本上我只是將主要值復制到范圍內沒有值的其他單元格。
我有同樣的問題,我發現沒有讀取單元格,而是第一個(有值)。 要解決此問題,請創建 PhpOffice\PhpSpreadsheet\Reader\Xls 的副本,並將合並單元格的 readMergedCells() 更改為 readDefault()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.