Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel - How to make part of the text bold

Tags:

php

phpexcel

How do you create a bold cell value using PHPExcel? I know I can use \n to add a carriage return within the text, but is there some kind of way to bold part of cell value? I also have tried using html formatting such as <b> or <strong> but it did not work.

like image 857
Ahmad Satiri Avatar asked Sep 10 '25 15:09

Ahmad Satiri


2 Answers

You can bold part of the text in a cell using rich text formatting, as described in section 4.6.37 of the developer documentation.

$objRichText = new PHPExcel_RichText();
$objRichText->createText('This text is ');

$objBold = $objRichText->createTextRun('bold');
$objBold->getFont()->setBold(true);

$objRichText->createText(' within the cell.');

$objPHPExcel->getActiveSheet()->getCell('A18')->setValue($objRichText);
like image 150
Mark Baker Avatar answered Sep 12 '25 03:09

Mark Baker


Yes you can bold a cell's value with the following code:

$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');

Hope this helps.

Source

like image 35
Eric LaForce Avatar answered Sep 12 '25 04:09

Eric LaForce