4

我正在使用 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/

4

2 回答 2

3

试试这个:

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

     }
于 2011-12-14T13:03:11.067 回答
3

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

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

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

$objPHPExcel->getActiveSheet()->getCell('A18')->setValue($objRichText);
于 2011-12-14T11:42:44.807 回答