使用PHPExcel将行和列中的单元格合并在一起

Tin*_*iny 17 php excel-2007 phpexcel

我需要按行合并Excel(xlsx)中的单元格,再使用列合并列PHPExcel.我尝试了以下内容.

$sheet->mergeCells("G".($row_count+1).":G".($row_count+4));             
$sheet->mergeCells("H".($row_count+1).":H".($row_count+4));             
$sheet->mergeCells("I".($row_count+1).":I".($row_count+4));     
Run Code Online (Sandbox Code Playgroud)

变量$row_count有一些不可预测的动态值,如25,50,75等(没有常规模式).

在此输入图像描述

它合并单元格,如前面的快照所示,可以在Note单元格的正下方看到.在按行合并这些单元格之后,我正在尝试按列合并它们,如下所示.

$sheet->mergeCells("G".($row_count+1).":I".($row_count+1));             
Run Code Online (Sandbox Code Playgroud)

但它不起作用.当我尝试打开excel文件时,它会要求确认(带有确认框)

Excel在"report.xlsx"中找到了不可读的内容.你想恢复这个工作簿的内容吗?如果您信任此工作簿的来源,请单击"是".

如何在Excel中按行和列合并单元格呢?

Mar*_*ker 23

合并只需要一个有效的单元格范围,如A1:B2,所以你的

$sheet->mergeCells("G".($row_count+1).":I".($row_count+1));
Run Code Online (Sandbox Code Playgroud)

应该没有任何问题.

你可以试试一个简单的测试用例来证明这会导致你的问题,而不是你的脚本中的其他东西

编辑

重新阅读您的问题之后:您的问题可能是您正在尝试合并已经属于合并范围的单元格,而不是合并每一行,然后尝试按列合并,尝试合并整个范围.

$sheet->mergeCells("G".($row_count+1).":I".($row_count+4));              
Run Code Online (Sandbox Code Playgroud)


Dar*_* ZD 9

还有一种用于细胞合并的方法

    /**
 * Set merge on a cell range by using numeric cell coordinates
 *
 * @param   int $pColumn1   Numeric column coordinate of the first cell
 * @param   int $pRow1      Numeric row coordinate of the first cell
 * @param   int $pColumn2   Numeric column coordinate of the last cell
 * @param   int $pRow2      Numeric row coordinate of the last cell
 * @throws  Exception
 * @return PHPExcel_Worksheet
 */
     public function mergeCellsByColumnAndRow($pColumn1 = 0, $pRow1 = 1, $pColumn2 = 0, $pRow2 = 1)
Run Code Online (Sandbox Code Playgroud)