46

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?

James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
Cheerio
  • 1,260
  • 6
  • 19
  • 37
  • Jared Farrish >> something like: http://dpaste.de/uXbH/raw/ – Cheerio Jul 25 '11 at 18:01
  • It looks like your headers aren't working. Make sure they are before any output whatsoever (even whitespace). – Jared Farrish Jul 25 '11 at 18:03
  • If you have Firebug or Chrome, you can also use the NET tab to inspect the headers that your browser is seeing. But from what that looks like, your browser seems to think it's plaintext and is trying to display it like a plaintext document. – Jared Farrish Jul 25 '11 at 18:05

3 Answers3

113

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.

James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
  • Hi Jahmic, I want to check whether two rows have been merged or not. I have 1000 of rows and if any two is merged in between then exit. Please can you help? – user3816325 Jul 21 '16 at 06:42
  • @user3816325 I haven't used these methods for a few years now, but I have no reason to believe anything is different. Make backups and give it a go. If your question is more complex, perhaps posting a new question is the way to go. – James John McGuire 'Jahmic' Jul 21 '16 at 14:46
  • It works fine, but the value is gone, how to keep the cell values after merging? – Anjan Biswas Jan 24 '19 at 08:39
  • @AnjanBiswas I haven't used this in quite a while, but I would suggest manually copying the values into separate PHP variables, format them the way you want, and then add that value back into the merged cell. BTW, the same thing will happen if you merge cells using the actual Excel application. So, by design. – James John McGuire 'Jahmic' Jan 24 '19 at 14:40
  • 1
    @Jahmic I already found the solution. Actually I used cell merging for bar chart. To add value = `$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 1, "2010");` cell merging = `$objPHPExcel->getActiveSheet()->mergeCells('A1:A5');` horizontal and vertical alignment for the cell = `$objPHPExcel->getActiveSheet() ->getStyle('A1:A5') ->getAlignment() ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet() ->getStyle('A1:A5') ->getAlignment() ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);` – Anjan Biswas Jan 25 '19 at 05:05
  • @AnjanBiswas Great work – Bang Andre Jul 07 '22 at 08:50
17

Try this

$objPHPExcel->getActiveSheet()->mergeCells('A1:C1');
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
4

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

ajshort
  • 3,684
  • 5
  • 29
  • 43
cks
  • 157
  • 1
  • 12