Mr Alb
Mr Alb

Reputation: 291

Write text between merged cells using phpExcel

I have this code from phpExel.

$objPHPExcel->setActiveSheetIndex(1)->mergeCells('A1:I1');

This code creates some blank space between columns A1 to I1. Just want to add text in the center blank space(A1:I1).

I am trying this code:

$objPHPExcel->setActiveSheetIndex(1)->mergeCells('A1:I1','add some text here...');

but this code does not work. Can anyone help me please?

Upvotes: 6

Views: 12908

Answers (2)

Bang Andre
Bang Andre

Reputation: 541

$objPHPExcel->getActiveSheet()->mergeCells('A1:A2');
$objPHPExcel->getActiveSheet()->getCell('A1')->setValue('Your lable');
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212402

You simply write the text value that you want to the top-left cell in the merged cell range

$objPHPExcel->setActiveSheetIndex(1)
    ->mergeCells('A1:I1');
$objPHPExcel->getActiveSheet()
    ->getCell('A1')
    ->setValue('This is the text that I want to see in the merged cells');

EDIT

To centre the text, use

$objPHPExcel->getActiveSheet()
    ->getStyle('A1')
    ->getAlignment()
    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

again, using the top-left cell of the range

Upvotes: 14

Related Questions