Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge Cell values with PHPExcel - PHP

I have a simple table like:

  - id  - first_name - last_name - email - phone  

I'm using PHPExcel to export my data in XLS format

      $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_name in one Cell

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?

like image 786
Cheerio Avatar asked Jul 25 '11 17:07

Cheerio


People also ask

How do I merge cells in PHPExcel?

$sheet = $workbook->getActiveSheet(); $sheet->setCellValue('A1','A pretty long sentence that deserves to be in a merged cell'); $sheet->mergeCells('A1:C1');

How do you insert row and column and merging cell explain briefly?

You can combine two or more table cells located in the same row or column into a single cell. For example, you can merge several cells horizontally to create a table heading that spans several columns. Select the cells that you want to merge. Under Table Tools, on the Layout tab, in the Merge group, click Merge Cells.

How do I change font size in PHPExcel?

$objPHPExcel->getActiveSheet()->getStyle("F1:G1")->getFont()->setFontSize(16);


2 Answers

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.

like image 132
Jahmic Avatar answered Oct 02 '22 07:10

Jahmic


Try this

$objPHPExcel->getActiveSheet()->mergeCells('A1:C1'); 
like image 36
Sadikhasan Avatar answered Oct 02 '22 05:10

Sadikhasan