7

I am using PHPExcel to create a xls file. I have merged the cells (B2:E2), but I want the text entered in B2 to be horizantally center aligned in the B2 cell only. It gets center aligned in the merged cell.

$sheet_obj->getStyle('B2')->getAlignment()->setHorizontal(
    PHPExcel_Style_Alignment::HORIZONTAL_CENTER
);
halfer
  • 19,824
  • 17
  • 99
  • 186
Neha Dangui
  • 597
  • 2
  • 13
  • 28
  • 4
    Can you do it in Excel itself? If you can't then it can't be done in PHPExcel. I can't really see how it would be possible. When merged, it's seen as a single cell, so centring would be in that context. – Kevin Nagurski Jun 02 '15 at 10:03
  • 1
    You can't. Though you could achieve a similar visual effect by merging cells C2:E2, and setting the border between B2 and C2 to match the cell background colour so that it looked as though it was part of the merge group – Mark Baker Jun 02 '15 at 11:14
  • Thank you for the suggestions :) – Neha Dangui Jun 03 '15 at 05:13
  • http://stackoverflow.com/questions/21285884/how-to-center-the-text-in-phpexcel-merged-cell – Chris P Aug 30 '16 at 08:54

1 Answers1

5

hope this helps

$sheet_obj->getActiveSheet()->getStyle('G2') ->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);