使用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>";
答案 0 :(得分:0)
为了检查单元格是否合并
首先,您可以使用getMergeCells函数来获取所有合并的单元格。
然后在该单元格列表中循环以检查您的单元格是否在该列表中。
总结:您可以使用此功能检查是否合并了单元格
// 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;
}
该代码是从this answer引用的
对于PhpSpreadSheet: