Search code examples
phpexcel

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?


Solution

  • 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