简体   繁体   English

使用PHPExcel按行和列合并Excel中的单元格

[英]Merging cells in Excel by rows and columns together using PHPExcel

I need to merge cells in Excel (xlsx) by rows and again by columns using PHPExcel .我需要使用PHPExcel按行并再次按列合并 Excel (xlsx) 中的单元格。 I tried the following.我尝试了以下方法。

$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));     

Where the variable $row_count has some unpredictable dynamic value like 25, 50, 75 and so on (no regular pattern).变量$row_count有一些不可预测的动态值,如 25、50、75 等(没有规则模式)。

在此处输入图片说明

It merges the cells as shown in the preceding snap shot as can be seen immediately below the Note cell.它合并了前面快照中显示的单元格,可以在Note单元格的正下方看到。 After merging these cells by rows, I'm trying to merge them by columns as follows.按行合并这些单元格后,我尝试按列合并它们,如下所示。

$sheet->mergeCells("G".($row_count+1).":I".($row_count+1));             

but it doesn't work.但它不起作用。 When I try to open the excel file, it asks for a confirmation (with a confirmation box)当我尝试打开 excel 文件时,它要求确认(带有确认框)

Excel found unreadable content in 'report.xlsx'. Excel 在“report.xlsx”中发现不可读的内容。 Do you want to recover the contents of this workbook?是否要恢复此工作簿的内容? If you trust the source of this workbook, click Yes.如果您信任此工作簿的来源,请单击是。

How to merge cells by rows and columns together in Excel then?那么如何在Excel中按行和列合并单元格?

Merging simply requires a valid range of cells like A1:B2, so your合并只需要一个有效范围的单元格,如 A1:B2,所以你的

$sheet->mergeCells("G".($row_count+1).":I".($row_count+1));

should work without any problem.应该可以正常工作。

Can you please experiment with a simple test case to prove that this is causing you a problem, and not something else in your script您能否尝试一个简单的测试用例来证明这给您带来了问题,而不是脚本中的其他问题

EDIT编辑

After rereading your question: Your problem may be that you're trying to merge cells that are already part of a merge range, rather than merging each row, then trying to merge by column, try merging the full rangein one go.重新阅读您的问题后:您的问题可能是您正在尝试合并已经属于合并范围的单元格,而不是合并每一行,然后尝试按列合并,尝试一次性合并整个范围。

$sheet->mergeCells("G".($row_count+1).":I".($row_count+4));              

There is one more method for cell merging还有一种单元格合并的方法

    /**
 * 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)
function cellsToMergeByColsRow($start = -1, $end = -1, $row = -1){
    $merge = 'A1:A1';
    if($start>=0 && $end>=0 && $row>=0){
        $start = PHPExcel_Cell::stringFromColumnIndex($start);
        $end = PHPExcel_Cell::stringFromColumnIndex($end);
        $merge = "$start{$row}:$end{$row}";
    }
    return $merge;
}

Addition to the case:案例补充:

$objPHPExcel->getActiveSheet()->mergeCells(cellsToMergeByColsRow(0,2,3))

I make a simple function to calc cells to merge by cols and row.我做了一个简单的函数来计算单元格以按列和行合并。

function cellsToMergeByColsRow($start = NULL, $end = NULL, $row = NULL){
    $merge = 'A1:A1';
    if($start && $end && $row){
        $start = PHPExcel_Cell::stringFromColumnIndex($start);
        $end = PHPExcel_Cell::stringFromColumnIndex($end);
        $merge = "$start{$row}:$end{$row}";

    }

    return $merge;
}

And call并打电话

$sheet->mergeCells(cellsToMergeByColsRow($col, $col+5, $row));

Thanks @Mark Baker谢谢@马克贝克

I was also looking solution for this question.我也在寻找这个问题的解决方案。 where i want to merge cell and put content (value) on that.我想合并单元格并在其上放置内容(值)。 After few search i got some solution on this.经过几次搜索,我得到了一些解决方案。 but did not checked because i am using Maatawebsite for get Excel file.但没有检查,因为我正在使用Maata 网站获取Excel文件。

But any one can try thing.. Solution is based on PHPExcel nit sure ,it will work on Maatawebsite .但是任何人都可以尝试一下.. 解决方案基于PHPExcel确定,它可以在Maata 网站运行

Source Link源链接

Merge from column A row 1 to column E row 1从 A 列第 1 行合并到 E 列第 1 行

$objPHPExcel->getActiveSheet()->mergeCells('A1:E1');

// add some text
$objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.'); 

Merge from column A row 1 to column E row 3从 A 列第 1 行合并到 E 列第 3 行

$objPHPExcel->getActiveSheet()->mergeCells('A1:E3');

// add some text
$objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.');

I checked maatawebsite document and they have same method mergeCells .我检查了 maatawebsite 文档,它们有相同的方法mergeCells so i think i would be work.所以我想我会工作。

This Solution from Maatawebste. Maatawebste 的这个解决方案。

$sheet->cells('A1:C1', function($cells) {
    $cells->setBorder('thin', 'thin', 'thin', 'thin');
});
$sheet->mergeCells('A1:C1');

Solution 2nd解决方案二

$sheet->setMergeColumn(array(
'columns' => array('A','B','C','D'),
'rows' => array(
array(2,3),
array(5,11),
)
));
$sheet -> mergeCellsByColumnAndRow($col1, $row1, col2, row2);

是函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM