使用phpspreadsheet合并细胞的检测



使用phpspreadsheet,当我从XLS表中读取数据时,我需要找出该单元是否与他人合并,如果是,请与之执行某些操作,如果不是,则什么都不做

目前我想到仅检查文本单元格之后的空数组元素的存在,但是该解决方案并不完全通用...

...
$inputFileName = $_FILES['uploadfile']["tmp_name"];
echo 'TMP-FILE-NAME: ' . $inputFileName;
$spreadsheet = IOFactory::load($inputFileName); //create new speedsheen object
$loadedSheetNames = $spreadsheet->getSheetNames(); //get name of Sheet
//and than print it
    //get Sheet Name
foreach ($loadedSheetNames as $sheetIndex => $loadedSheetName) {
  $sheet = $spreadsheet->getSheet($sheetIndex);
  echo "<table border="1">";
  $rows = $sheet->toArray();
   **$mergeCell = $sheet->getMergeCells(); // - This is the answer to my question**
foreach ($rows AS $row) {
echo "<tr>";
    foreach ($row AS $cell) {
        echo "<td>" . $cell . "</td>";
    }
    }
     echo '<br/>';
}
 echo "</table>";

以检查单元格是否合并

  1. 首先,您可以使用getMergeCells功能获取所有合并的单元格。

  2. 然后在该单元格中进行循环以检查您的单元格在该列表中或不在该列表中。

总结:您可以使用此功能检查单元格合并

// Check cell is merged or not
function checkMergedCell($sheet, $cell){
    foreach ($sheet->getMergeCells() as $cells) {
        if ($cell->isInRange($cells)) {
            // Cell is merged!
            return true;
        }
    }
    return false;
}

从此答案中引用了代码

phpspreadsheet:

  • getMergeCells:https://phpoffice.github.io/phpspreadsheet/1.2.0/phpoffice/phpoffice/phpspreadsheet/worksheet/worksheet/worksheet.html#method_getmergecells
  • isInrange:https://phpoffice.github.io/phpspreadsheet/1.2.0/phpoffice/phpspreadsheet/cell/cell/cell/cell.html#method_isinrange

最新更新