A
A
Alexander2020-04-19 21:42:37
phpspreadsheet
Alexander, 2020-04-19 21:42:37

How to make part of text bold in phpexcel?

Hello colleagues!
I've been fighting for two days, so I decided to write here.
There is a php script that generates an xlsx file. You need to insert text into one of the cells and highlight part of it in bold.
The text is formed according to the principle: Fixed text + variable $out->suggestions[0]->values

​​Did according to the textbook:

$objRichText = new PHPExcel_RichText();
$objRichText->createText(''); //может и не нужен
$objPayable = $objRichText->createTextRun('Фиксированный текст');
$objPayable->getFont()->setBold(true);
$objRichText->createText($out->suggestions[0]->values); //через переменную типа $test = $out->suggestions[0]->values; тоже не идет


Further output: $page->setCellValue("B{$num2}", $objRichText);

I googled everything, as a result, the fixed text is displayed, the dynamic part is not .. If I replace the dynamic part with text, everything is OK, but I need it with the change of data.

And even when entering text, it does not respond to the conditions in terms of the font (font, size)

Who faced this, how to solve it, tell me.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-05-10
@SilimAl

In the end, I did everything through PHPExcel_RichText ().
I redid the output into a cell and everything worked as it should, I think this was the problem.
I made two createTextRun to be able to specify the font in the first and second parts forcibly, otherwise the font and size changed in an incomprehensible way.

$objKOMYrichText = new PHPExcel_RichText();
$objKOMY = $objKOMYrichText->createTextRun('Кому: ');
$objKOMY->getFont()->setBold(true);
$objKOMY->getFont()->setName('Times New Roman');
$objKOMY->getFont()->setSize(16);                           
$objKOMY2 = $objKOMYrichText->createTextRun($out->suggestions[0]->value);
$objKOMY2->getFont()->setName('Times New Roman');
$objKOMY2->getFont()->setSize(16);     
$xls->getActiveSheet()->getCell("B{$num2}")->setValue($objKOMYrichText);

D
Daria Motorina, 2020-04-19
@glaphire

PHPExcel has been deprecated for a long time, but you can get examples from unit tests, there is one for rich text ( link ).
UPD. By the way, the example is quite in the subject, because there you can prepare html code and just in the template wrap only what you need with the <b> tag, and not all the text

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question