Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpExcel , how to write superscript in an excel

I`m using the PHPExcel class ,to write in an excel file.

My question is: how to write a data in a cell with superscript,if my value is ( M<sup>3</sup>) to M3 (in excel)

  // $data['dataLashing'][$i]['units_name'] - it`s a value from db in LOOP

$getExcelObject
            ->mergeCells("A$startRow:E$startRow")->setCellValue("A$startRow",$data['dataLashing'][$i]['material'])
            ->mergeCells("F$startRow:G$startRow")->setCellValue("F$startRow",$data['dataLashing'][$i]['units_name'])
            ->mergeCells("H$startRow:I$startRow")->setCellValue("H$startRow",$data['dataLashing'][$i]['quantity']);

http://phpexcel.codeplex.com/

like image 906
Dezigo Avatar asked Oct 25 '25 20:10

Dezigo


2 Answers

Try this one:

      $objRichText = new PHPExcel_RichText();
      $objRichText->createText($data['dataLashing'][$i]['units_name']);

   //condition your html tag
      if(preg_match('/<sup>(.+)<\/sup>/i', $data['dataLashing'][$i]['units_name'],$result)) {
         $objRichText = new PHPExcel_RichText();
         $objCubed = $objRichText->createText(preg_replace('/<sup>(.+)<\/sup>/i', '', $data['dataLashing'][$i]['units_name']));
         $objCubed = $objRichText->createTextRun($result[1]);
         $objCubed->getFont()->setSuperScript(true);
      }      
      $getExcelObject
       ->mergeCells("A$startRow:E$startRow")->setCellValue("A$startRow",$data['dataLashing'][$i]['material'])
        ->mergeCells("F$startRow:G$startRow")->setCellValue("F$startRow",$objRichText)
        ->mergeCells("H$startRow:I$startRow")->setCellValue("H$startRow",$data['dataLashing'][$i]['quantity']);

     }
like image 140
Oyeme Avatar answered Oct 27 '25 09:10

Oyeme


You need to populate the cell with rich text:

$objRichText = new PHPExcel_RichText();
$objRichText->createText('M');

$objCubed = $objRichText->createTextRun('3');
$objCubed->getFont()->setSuperScript(true);

$objPHPExcel->getActiveSheet()->getCell('A18')->setValue($objRichText);
like image 31
Mark Baker Avatar answered Oct 27 '25 09:10

Mark Baker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!