使用 PHPExcel 合并单元格值 - PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6820250/
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-26 01:22:05  来源:igfitidea点击:

Merge Cell values with PHPExcel - PHP

phpphpexcel

提问by Cheerio

I have a simple table like:

我有一个简单的表,如:


- id 
- first_name
- last_name
- email
- phone

I'm using PHPExcelto export my data in XLS format

我正在使用PHPExcel以 XLS 格式导出我的数据


    $rowNumber = 1;
    while ($row = mysql_fetch_row($result)) {
       $col = 'A';
       foreach($row as $cell) {
          $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
          $col++;
       }
       $rowNumber++;
   }

Now I want to merge the two fields first_name& last_namein one Cell

现在,我要合并的两个领域first_name-last_name在一个单元格

I tried:

我试过:


$rowNumber = 1;
   while ($row = mysql_fetch_row($result)) {
   $objPHPExcel->getActiveSheet()->setCellValue('A'.$rowNumber,$row['id'])
                                 ->setCellValue('B'.$rowNumber,$row['first_name'])
                                 ->setCellValue('C'.$rowNumber,$row['last_name']);                                                                  
   $rowNumber++;
}

But I get errors and don't works. Any help?

但是我收到错误并且不起作用。有什么帮助吗?

回答by Jahmic

There is a specific method to do this:

有一种特定的方法可以做到这一点:

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

You can also use:

您还可以使用:

$objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:C1');

That should do the trick.

这应该够了吧。

回答by Sadikhasan

Try this

尝试这个

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

回答by cks

$this->excel->setActiveSheetIndex(0)->mergeCells("A".($p).":B".($p));for dynamic merging of cells

$this->excel->setActiveSheetIndex(0)->mergeCells("A".($p).":B".($p));用于细胞的动态合并