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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10909598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:19:23  来源:igfitidea点击:

Merging cells in Excel by rows and columns together using PHPExcel

phpexcel-2007phpexcel

提问by Tiny

I need to merge cells in Excel (xlsx) by rows and again by columns using PHPExcel. I tried the following.

我需要使用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));     

Where the variable $row_counthas some unpredictable dynamic value like 25, 50, 75 and so on (no regular pattern).

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

enter image description here

在此处输入图片说明

It merges the cells as shown in the preceding snap shotas can be seen immediately below the Notecell. After merging these cells by rows, I'm trying to merge them by columns as follows.

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

$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'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.

Excel 在“report.xlsx”中发现无法读取的内容。是否要恢复此工作簿的内容?如果您信任此工作簿的来源,请单击是。

How to merge cells by rows and columns together in Excel then?

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

回答by Mark Baker

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

回答by Darkhan ZD

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)

回答by Alexej

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

回答by jashk

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

谢谢@马克贝克

回答by pankaj

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 Maatawebsitefor get Excelfile.

我也在寻找这个问题的解决方案。我想合并单元格并在其上放置内容(值)。经过几次搜索,我得到了一些解决方案。但没有检查,因为我正在使用Maata 网站获取Excel文件。

But any one can try thing.. Solution is based on PHPExcelnit 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. so i think i would be work.

我检查了 maatawebsite 文档,它们有相同的方法mergeCells。所以我想我会工作。

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

回答by jmrobin92

$sheet -> mergeCellsByColumnAndRow($col1, $row1, col2, row2);

is the function.

是函数。