phpExcel,如何在excel中写上标

时间:2011-12-14 11:15:51

标签: php phpexcel superscript

我正在使用PHPExcel类来编写excel文件。

我的问题是:如果我的值为( M<sup>3</sup>)到M 3 (在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/

2 个答案:

答案 0 :(得分:2)

试试这个:

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

     }

答案 1 :(得分:1)

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

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

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

$objPHPExcel->getActiveSheet()->getCell('A18')->setValue($objRichText);