问题:section中使用addText中文乱码
方案:找到PHPWord Section.php文件 ,大概在111行,更改为
public function addText($text, $styleFont = null, $styleParagraph = null) { // $givenText = utf8_encode($text); // $givenText = iconv('gbk', 'utf-8', $text); $givenText = $text; $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph); $this->_elementCollection[] = $text; return $text; }
问题:中文字体不支持
解决:1、打开文件/Writer/Word2007/Base.php
2、找到函数 _writeTextStyle
添加:大概310行
// Font if($font != 'Arial') { $objWriter->startElement('w:rFonts'); $objWriter->writeAttribute('w:eastAsia', $font);// 添加这行 $objWriter->writeAttribute('w:ascii', $font); $objWriter->writeAttribute('w:hAnsi', $font); $objWriter->writeAttribute('w:cs', $font); $objWriter->endElement(); }
问题:页眉不支持中文
解决:1、打开/PHPWord/Section/Header.php文件
2、找到函数addText,大概74行,做下述修改
public function addText($text, $styleFont = null, $styleParagraph = null) { // $givenText = utf8_encode($text); $givenText = $text; $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph); $this->_elementCollection[] = $text; return $text; }
示例代码:
require_once 'PHPWord.php'; $PHPWord = new PHPWord(); $section = $PHPWord->createSection(); //添加页眉 $header = $section->createHeader(); $table = $header->addTable(); $table->addRow(); $table->addCell(4500)->addText('This is the header'); $table->addCell(4500)->addImage('_earth.jpg',array('width'=>50,'height'=>50,'align'=>'right')); //添加页脚 $footer = $section->createFooter(); $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.',array('align'=>'center')); //添加一些文本 $section->addTextBreak(); $section->addText('Some Text ...'); //保存文件 $objWriter = PHPWord_IOFactory::createWriter($PHPWord,'Word2007'); $objWriter->save('HeaderFooter.docx');