Home » excel » php – Read Excel Data (Rich Excel Data)

php – Read Excel Data (Rich Excel Data)

Posted by: admin April 23, 2020 Leave a comment

Questions:

I want to read data form an Excel file and show in html. I know there are several libraries like PHPExcel, PHP-ExelReader that do so.

But, I also want to keep the format that was in excel file.
For example :

−19×10−17 J should be parsed as −19&times;10<sup>−17</sup> J And
m1r1 : m2r2 as m<sub>1</sub>r<sub>1</sub> : m<sub>2</sub>r<sub>2</sub>.

Is there any library that could do so? I have googled but did not get any solution for this. Please recommend some libraries or any tutorial for this task.

Any help will be highly appreciated.

Update – I also want to store data in database.

How to&Answers:

Got the solution. It’s PHPExcel.

While fetching values from cells, what i did is as following.

$value = $cell->getValue();
if($value instanceof PHPExcel_RichText)
{
    $cellValueAsString = '';
    $elements = $value->getRichTextElements();
    foreach ($elements as $element)
    {
        if ($element instanceof PHPExcel_RichText_Run) 
        {
            if ($element->getFont()->getSuperScript()) 
            {
                $cellValueAsString .= '<sup>';
            } 
            else if ($element->getFont()->getSubScript()) 
            {
                $cellValueAsString .= '<sub>';
            }
        }

        $cellText = $element->getText();
        $cellValueAsString .= htmlspecialchars($cellText);
        if ($element instanceof PHPExcel_RichText_Run) 
        {
            if ($element->getFont()->getSuperScript()) 
            {
                $cellValueAsString .= '</sup>';
            } 
            else if ($element->getFont()->getSubScript()) 
            {
                $cellValueAsString .= '</sub>';
            }
        }
    }

    $value = $cellValueAsString;
}