如何在excel中写上标


phpExcel , how to write superscript in an excel

我正在使用PHPExcel类,在excel文件中写入

我的问题是:如何在上标单元格中写入数据,如果我的值是( M<sup>3</sup>)到M3(在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/

试试这个:

      $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']);
     }

您需要用富文本填充单元格:

$objRichText = new PHPExcel_RichText();
$objRichText->createText('M');
$objCubed = $objRichText->createTextRun('3');
$objCubed->getFont()->setSuperScript(true);
$objPHPExcel->getActiveSheet()->getCell('A18')->setValue($objRichText);