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.
Asked
Active
Viewed 5.2k times
5 Answers
56
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);

Mark Baker
- 209,507
- 32
- 346
- 385
-
Hi Mark thanks for your answer, this is working for Excel2007 object writer. But it cannot working for excel5. – Ahmad Satiri Nov 10 '11 at 01:53
-
Rich text isn't yet supported for the Excel5 Writer – Mark Baker Nov 10 '11 at 09:02
-
1Thanks for the the info and thanks for the great PHPExcel library. – Ahmad Satiri Nov 11 '11 at 01:19
-
Support for Rich Text in the Excel5 Writer was added in version 1.7.7 (2012-05-19) – Mark Baker Apr 30 '15 at 11:58
-
Good. I have the following code. It sets data statically. It happens one case of 100 produces broken excel file. http://paste.debian.net/170182/ If I removed this code - no problem happens. The problem is that we want text in the single cell to have different formatting so we can't avoid using richtext. Thanks – Tebe Apr 30 '15 at 12:07
-
1Then ask as a separate question, and post the minimum code to demonstrate the problem – Mark Baker Apr 30 '15 at 12:42
-
Thank you. Now Php excel become PHP Spreadsheet, its document is here https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#add-rich-text-to-a-cell – Thang Nguyen May 29 '19 at 07:02
14
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.

Eric LaForce
- 2,125
- 15
- 24
-
1It did make the whole cell value to be bold. But that is not what i am looking for. What I was asking is , just to make some part of cell value become bold, let say just "hello" or "world". – Ahmad Satiri Nov 10 '11 at 01:21
-
I see, try out @Mark's answer in that case, it looks more appropriate. – Eric LaForce Nov 10 '11 at 01:24
10
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B1', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D1', 'world!');
for single cell use:
$objPHPExcel->getActiveSheet()->getStyle("A1")->getFont()->setBold(true);
for multiple cells use:
$objPHPExcel->getActiveSheet()->getStyle("A1:D1")->getFont()->setBold(true);

Ashwin
- 450
- 1
- 6
- 17
-
This will only make the whole value of cell become bold. You need to use RichText as Mark Baker Suggest. And Btw, He is one of PHPExcel Developers. – Ahmad Satiri Nov 22 '12 at 09:03
-
I have upvoted this answer even though it doesn't answer OP's question, but it helped for my cause. – Frodik Mar 20 '14 at 09:52
-
@Ashwin Thanks for your answer. `$objPHPExcel->getActiveSheet()->getStyle("A1")->getFont()->setBold(true); I have used your code in PHPExcel_Writer_Excel2007. It is working so thank you so much. ` – Bhavin Thummar Jan 24 '19 at 07:42
7
You can use the HTML helper class in PHPExcel to bold text.
$htmlHelper = new \PHPExcel_Helper_HTML();
$html = "<b> Bold Text!! </b>";
$rich_text = $htmlHelper->toRichTextObject($html);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $rich_text);

Syam
- 409
- 5
- 8
-
This helped me! The rich text method caused a (recoverable) xml error. – Klompenrunner Sep 12 '17 at 01:00
-
This is very useful solution, BUT! :) - I`ve got problem with UTF-8 encoding for html code passing trhu Helper_HTML. So, I`ve found this working fix for that case: $wizard = new PHPExcel_Helper_HTML; $richText = $wizard->toRichTextObject('' . $html1); – Pavel Mar 23 '22 at 07:10