2011-12-14 96 views
2

我使用PHPExcel类写入excel文件。
phpExcel,如何在excel中写上标

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

试试这个:

 $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

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

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

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

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

是它了工作,但在数据库中,我的价值观是内容类似这样的中号一个html标记。我想这样做动态 – Dezigo 2011-12-14 12:10:25

+0

转换为HTML标签上标.. – Dezigo 2011-12-14 12:11:05