PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 4, 2022

[FIXED] How to get html of a cell using PHPExcel?

 October 04, 2022     format, import, php, phpexcel     No comments   

Issue

I've tried to import an xlsx file to array, but noticed, that it contains simple text whereas if I open it in OpenOffice, I can see some words in the cell are bold.

#items: array:2 [
    "url" => "theurl"
    "meta_title" => "text with one bold word"
]

Is there a way to get somehow text with <b>one</b> bold word?

PHPExcel wiki says nothing about it. Its valueBinder applies only to csv and html files.


Solution

Thanks to Mark Baker. The HTML Writer code helps to create simple converter. Usage:

$richtextService = new RichTextService();

$value = $reader->getActiveSheet()->getCell('F2')->getValue();
$html = $richtextService->getHTML($value); // html in it

And the class:

namespace App\Services;

use PHPExcel_RichText;
use PHPExcel_RichText_Run;
use PHPExcel_Style_Font;

class RichTextService{

    /**
     * Takes array where of CSS properties / values and converts to CSS stri$
     *
     * @param array
     * @return string
     */
    private function assembleCSS($pValue = array())
    {
        $pairs = array();
        foreach ($pValue as $property => $value) {
            $pairs[] = $property . ':' . $value;
        }
        $string = implode('; ', $pairs);
        return $string;
    }

    /**
     * Create CSS style (PHPExcel_Style_Font)
     *
     * @param    PHPExcel_Style_Font        $pStyle            PHPExcel_Style_Font
     * @return    array
     */
    private function createCSSStyleFont(PHPExcel_Style_Font $pStyle)
    {
        // Construct CSS
        $css = array();
        // Create CSS
        if ($pStyle->getBold()) {
            $css['font-weight'] = 'bold';
        }
        if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE && $pStyle->getStrikethrough()) {
            $css['text-decoration'] = 'underline line-through';
        } elseif ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE) {
            $css['text-decoration'] = 'underline';
        } elseif ($pStyle->getStrikethrough()) {
            $css['text-decoration'] = 'line-through';
        }
        if ($pStyle->getItalic()) {
            $css['font-style'] = 'italic';
        }
        $css['color']       = '#' . $pStyle->getColor()->getRGB();
        $css['font-family'] = '\'' . $pStyle->getName() . '\'';
        $css['font-size']   = $pStyle->getSize() . 'pt';
        return $css;
    }

    /**
     * 
     * @param PHPExcel_RichText|string $value
     * @return string
     */
    public function getHTML($value) {
        if(is_string($value)) {
            return $value;
        }

        $cellData = '';
        if ($value instanceof PHPExcel_RichText) {
            // Loop through rich text elements
            $elements = $value->getRichTextElements();
            foreach ($elements as $element) {
                // Rich text start?
                if ($element instanceof PHPExcel_RichText_Run) {
                    $cellData .= '<span style="' . $this->assembleCSS($this->createCSSStyleFont($element->getFont())) . '">';
                    if ($element->getFont()->getSuperScript()) {
                        $cellData .= '<sup>';
                    } elseif ($element->getFont()->getSubScript()) {
                        $cellData .= '<sub>';
                    }
                }
                // Convert UTF8 data to PCDATA
                $cellText = $element->getText();
                $cellData .= htmlspecialchars($cellText);
                if ($element instanceof PHPExcel_RichText_Run) {
                    if ($element->getFont()->getSuperScript()) {
                        $cellData .= '</sup>';
                    } elseif ($element->getFont()->getSubScript()) {
                        $cellData .= '</sub>';
                    }
                    $cellData .= '</span>';
                }
            }
        }

        return str_replace("\n", "<br>\n", $cellData);
    }
}


Answered By - shukshin.ivan
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing