如何使用 PhpSpreadsheet 检测合并单元格
How detect is cell merged using PhpSpreadsheet
使用 PhpSpreadsheet,
当我从 xls table 读取数据时,我需要找出这个单元格是否与其他单元格合并,
如果是,则对其进行某些操作,如果不是,则什么都不做
目前我只想检查文本单元格后是否存在空数组元素,但这个解决方案不是很通用...
...
$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>";
为了检查单元格是否被合并
首先,您可以使用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:
使用 PhpSpreadsheet, 当我从 xls table 读取数据时,我需要找出这个单元格是否与其他单元格合并, 如果是,则对其进行某些操作,如果不是,则什么都不做
目前我只想检查文本单元格后是否存在空数组元素,但这个解决方案不是很通用...
...
$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>";
为了检查单元格是否被合并
首先,您可以使用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: