PHPSpreadsheet 从单元格中的文本读取样式

标签 php phpspreadsheet

我有以下单元格数据:

bla bla bla abcd efgh ijkl

注意,abcd 是粗体,efgh 是斜体,ijkl 同时是粗体和斜体。

我想读取整个单元格的数据,并将其保存在数据库中,这样我就可以按原样(即使用那些样式)将其呈现给最终用户。 我搜索了文档,我只能修改这段代码:

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

但它的问题是,只有当整个单元格文本都是粗体时,这才有效,而在我的例子中,只有一部分是粗体。

有人知道怎么解决这个问题吗?谢谢!

最佳答案

具有多种格式的单元格使用 \PhpOffice\PhpSpreadsheet\RichText\RichText(请参阅文档中的 Add rich text to a cell)。

要获取所需的信息,您可以使用如下内容:

$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());
       }
}

关于PHPSpreadsheet 从单元格中的文本读取样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49371941/

相关文章:

php - 对象数组的排序算法

php soap maxoccurs=无限

PhpSpreadsheet 使用循环填充单元格

PHPSpreadsheet 和 CodeIgniter -> 消息 : syntax error, 意外 'use' (T_USE)

php - 是否可以使用 PhpSpreadSheet 在现有电子表格中写入?

php - 如何为 PHP 7 和 MAMP 启用 Webp

PHP、MYSQL、Echo 在单列中循环行

php - 为什么我的 detach() 和 delete() 不起作用?

php - 使用 Laravel 5.5 使用带有默认数据的 PhpSpreadsheet 创建 Excel 文件