Tag Archives: cells

PHPExcel: How to Bold a Cell’s Value CodeUnit 14 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 tip is on how to embolden a cell’s value – in other words how to replace that big B bold button with some code! ;)

Now the recommended method for applying styles to cells or cell ranges is by making use of style arrays to set a number of styles all at once, mostly because setting styles one by one can turn out to be a little resource intensive.

So in keeping with this then, here is how you would make a cell’s contents bold:


$workbook = new PHPExcel;
$sheet = $workbook->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World');
$styleArray = array(
'font' => array(
'bold' => true
)
);
$sheet->getStyle('A1')->applyFromArray($styleArray);
$writer = new PHPExcel_Writer_Excel5($workbook);
header('Content-type: application/vnd.ms-excel');
$writer->save('php://output');

And there you go, as simple as that!

Related Link: http://phpexcel.codeplex.com

PHPExcel: How to Set the Width of Cells CodeUnit 12 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 tip is on how to specify or set the width of individual columns in your worksheet.

In order to do this, we make use of the handy setWidth function that allows us to specify the width of our targeted column using the standard Excel column width units.

So let’s see this in code then:

$workbook = new PHPExcel;
$sheet = $workbook->getActiveSheet();
$sheet->getColumnDimension('A')->setWidth(15);
$sheet->getColumnDimension('B')->setWidth(30);
$sheet->getColumnDimension('C')->setWidth(45);
$writer = new PHPExcel_Writer_Excel5($workbook);
header('Content-type: application/vnd.ms-excel');
$writer->save('php://output');

And there you go, as simple as that!

Related Link: http://phpexcel.codeplex.com

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.

Nifty.

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');
$sheet->mergeCells('A1:C1');
$writer = new PHPExcel_Writer_Excel5($workbook);
header('Content-type: application/vnd.ms-excel');
$writer->save('php://output');

And there you go, as simple as that!

Related Link: http://phpexcel.codeplex.com