简体   繁体   中英

PHPSpreadsheet read styles from text in a cell

I'm having the following cell data:

bla bla bla abcd efgh ijkl

Notice, abcd is bold, efgh is italic, and ijkl is both bold and italic at the same time.

I would like to read the data for this entire cell, and save it in the database, so that I can then present it to the end-user as is (meaning with those styles). I've searched the docs, and I only could adapt this code:

$path = $this->get('kernel')->getRootDir() . '/../intrebari.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($path);
$bold = $spreadsheet->getActiveSheet()->getStyle('A3')->getFont()->getBold();

But the problem with it is that this works only if the entire cell text is bold, and in my case, only part of it is bold.

Anyone knows how to solve this issue? Thanks!

Cell with multiple formatting is instance of \\PhpOffice\\PhpSpreadsheet\\RichText\\RichText https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#add-rich-text-to-a-cell

To get information needed you can use something like this:

$cellValue =  $spreadsheet->getActiveSheet()->getCell('A3')->getValue();

if ($cellValue instanceof PhpOffice\PhpSpreadsheet\RichText\RichText) {
       foreach ($cellValue->getRichTextElements() as $richTextElement) {
                var_dump($richTextElement->getText());
                var_dump($richTextElement->getFont()->getBold());
       }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM