代码之家  ›  专栏  ›  技术社区  ›  Dan Costinel

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

  •  2
  • Dan Costinel  · 技术社区  · 7 年前

    我有以下手机数据:

    布拉布拉布拉布拉布拉 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();
    

    但问题是,只有当整个单元格文本都是加粗的,并且在我的情况下,只有部分单元格文本是加粗的,这才有效。

    有人知道如何解决这个问题吗?谢谢

    1 回复  |  直到 7 年前
        1
  •  6
  •   Nicolapps    3 年前

    使用多种格式的单元格 \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());
           }
    }