0

Uploading Excel file is ok and not an issue

gone through many articles on uloading excel to mysql Tried

http://phpexcel.codeplex.com/

http://sourceforge.net/projects/phpexcelreader/

https://stackoverflow.com/a/7889220/1026905 (really nice encoding class)

MY issue is i cannot upload the chracter seen in excel file as it is

superscript part is getting converted Example in

∴ tn = n² + 2n + 1 + 2

n² ... gets converted as it is
∴ .... get converted as it is

all characters like ∠Ωπ √ ∞ ≅⊥∫∪∴≈≡⊆μ I could get converted as it is

Only issue is tn, t1, t2, t3 i am unable to get n,1,2,3 as subscript (cannot be displayed here but you can view in the image

 tn ... should be t<sub>n</sub>

... in html format n should be subscript 

I am not getting this as it is

Community
  • 1
  • 1

2 Answers2

2

Using PHPExcel, you can identify if a cell contains rich text because a getValue() call will return a PHPExcel_RichText object rather than a string/integer/float/boolean scalar type.

You can then loop through the collection of PHPExcel_RichText_Run objects for that PHPExcel_RichText object looking at the style associated with it:

$cellValueAsString = '';
$elements = $cell->getValue()->getRichTextElements();
foreach ($elements as $element) {
    // Rich text start?
    if ($element instanceof PHPExcel_RichText_Run) {
        if ($element->getFont()->getSuperScript()) {
            $cellValueAsString .= '<sup>';
        } else if ($element->getFont()->getSubScript()) {
            $cellValueAsString .= '<sub>';
        }
    }
    // Convert UTF8 data to PCDATA
    $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>';
        }
    }
}

would be a simple block of code to turn the rich text cell content into simple HTML markup showing superscripted and subscripted characters

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Thanks $cell = $worksheet->getCellByColumnAndRow($col, $row); $val = $cell->getValue(); is the cell value I am getting I tried $elements = $cell->getValue()->getRichTextElements(); foreach ($elements as $element) { // Rich text start? but getting error Call to a member function getRichTextElements() on a non-object in D:\EasyPHP-5.3.8.0\www\maratheclasses\PHPexcel_178\php178reader.php on line 27 – vikas Athavale Jan 28 '13 at 18:14
  • You need to check if the value returned from the $val = $cell->getValue(); call is a rich text object before parsing it in this way; if you're just getting a normal scalar returned (e.g the cell value is a simple numeric) then you will get this error if you try to parse it as rich text – Mark Baker Jan 28 '13 at 19:29
  • I had included PHPExcel/cell/DataType.php and tried command echo "
    find data type :".dataTypeForValue($val); but getting error Call to undefined function dataTypeForValue() in... humble request pls guide which class to include and hot ot get value returned from $val = $cell->getValue();
    – vikas Athavale Jan 29 '13 at 02:29
  • I tried examples in directory Documentation/Examples/ it seems i am not getting proper way .. thanks in advance – vikas Athavale Jan 29 '13 at 03:38
0

Please try this code to read text with superscript from excel file using PHPExcel.

`/** PHPExcel_IOFactory */
require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php';


echo date('H:i:s') , " Load from Excel2007 file" , EOL;
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("Book5.xlsx");

 echo date('H:i:s') , " Iterate worksheets by Row" , EOL;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    echo 'Worksheet - ' , $worksheet->getTitle() , EOL;
     
    foreach ($worksheet->getRowIterator() as $row) {
        echo '    Row number - ' , $row->getRowIndex() , EOL;

        $cellIterator = $row->getCellIterator();
        $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
        foreach ($cellIterator as $cell) {
            if (!is_null($cell)   ) { 
                
                $orginaltext = $cell->getValue() ;
                  if (is_object($orginaltext)) {
                    $cellValueAsString = '';
                    $cellValueAsString1 = '';
                
                 if ($orginaltext->getRichTextElements() ) { 
                    $elements = $cell->getValue()->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>';
                            }
                        }
                    }else{
                        $cellText = $element->getText();
                        $cellValueAsString .= htmlspecialchars($cellText);
                    }
                    
                }
                 echo '        Cell - ' , $cell->getCoordinate() , ' - ' , nl2br($cellValueAsString) , EOL;
                     
                } 
                 }
                 else{
                echo '        Cell - ' , $cell->getCoordinate() , ' - ' , nl2br($cell->getCalculatedValue()) , EOL;
                 }
            }
        }
    }
}
 `

Also you can use the below code to get specific cell from Excel instead of row iteration. $cell = $sheet->getCellByColumnAndRow(11,2); This can be used to select purticular row.

meginjoy
  • 151
  • 1
  • 1
  • 7