PHPExcel: How to Merge Cells CodeUnit 09 JUL 2010

PHPExcel is a phenomenal Excel generating library for PHP which I have mentioned on this site numerous times before, and which is currently my defacto way for quickly knocking out all those annoying Excel downloads that clients always seem to MUST have!

Today’s quick code hint deals with how one sets about merging a cell range using PHPExcell. Unfortunately for me though, the solution is so simple that this will only take one line of my time! :P

So what is the solution then?

Well PHPExcel features a nifty little function named mergeCells, and feeding it a range results in that range being merged into one great big cell.


In code it would look a little something like this:

$workbook = new PHPExcel;
$sheet = $workbook->getActiveSheet();
$sheet->setCellValue('A1','A pretty long sentence that deserves to be in a merged cell');
$writer = new PHPExcel_Writer_Excel5($workbook);
header('Content-type: application/');

And there you go, as simple as that!

Related Link:

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.

  • Satiri A

    thanks for your articles.

  • Zdarsky Peter

    Thanks for your posts about PHPExcel

  • Adwfa

    Chevere :)

  • Rizaldi Maulidia

    YES….. I was looking for this.

    Thank you for sharing. :D