1、解決編碼問題,PHPword 會對輸入的文字進行utf8_encode編碼轉(zhuǎn)化,如果你使用GBK、GB2312或者utf8編碼的話就會出現(xiàn)亂碼,如果你用utf8編碼,就查找類庫中所有方法中的 utf8_encode 轉(zhuǎn)碼將其刪除,如果你采用GBK或者GB2312編碼,使用iconv進行編碼轉(zhuǎn)換。2、解決中文字體支持,在writer/word2007/base.php中 312行添加 $objWriter->writeAttribute('w:eastAsia',$font)3、啟動php zip支持,windows環(huán)境下在php配置文件php.ini中,將extension=php_zip.dll前面的分號“;”去除;(如果沒有,請?zhí)砑觘xtension=php_zip.dll此行并確保php_zip.dll文件存在相應(yīng)的目錄),然后同樣在php.ini文件中,將 zlib.output_compression = Off 改為zlib.output_compression = On ;
計量單位:緹(twips)
首先解釋一下PHPWord最基本的計量單位:“緹”(twips),我們常常在文件中看到或使用計量單位“緹”,它是開源辦公軟件中最基本的計量單位,“緹”是"TWentieth of an Inch Point"的簡寫,意思 1/20磅,與其他常用劑量單位的換算是1緹=1/1,440英寸,1緹=1/567厘米,1緹=1/15像素
下面是實際的使用案例:
require_once 'word/PHPWord.php';
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
// Define table style arrays
$styleTable = array('borderSize'=> 0, 'borderColor' => 'ffffff', 'cellMargin' => 80 , 'marginLeft' => 1134,'marginRight' => 1134,'marginTop' => 1134,'marginBottom' => 1134,'align' => 'right','valign' => 'right');
$styleFirstRow = array('borderBottomSize' => 18, 'borderBottomColor' => '000000', 'bgColor' => '666666','valign' => 'center');
// Define cell style arrays
$styleCell = array('align' => 'center','borderSize' => 1, 'bgColor' => ffffff, );
$styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);
// Define font style for first row
$fontStyle = array('bold'=>true, 'valign' => 'right' , 'align' => 'right' , 'size'=>24);
$fontStyles = array('color'=>'000000', 'size'=>18, 'bold'=>true , 'align' => 'right' , 'valign' => 'right');
$titleStyles = array('color'=> 000000 ,'size'=>24);
$PHPWord->addFontStyle('myOwnStyle', $fontStyles);
// Add table style
$PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
// Add table
$table = $section->addTable('myOwnTableStyle');
// Add row
$result = $this->db->query($sq1);
$i=0;
$table->addRow(100);
$table->addCell(9600)->addText('各科室工作完成情況', array( 'size' => 30 ,'bold'=>true) , array( 'align' => 'center'));
$table->addRow(100);
$table->addCell(1500, $styleCell )->addText('單位', $fontStyles , array( 'align' => 'center'));
$table->addCell(3300, $styleCell)->addText('標(biāo)題',$fontStyles, array( 'align' => 'center'));
$table->addCell(3300, $styleCell)->addText('內(nèi)容', $fontStyles, array( 'align' => 'center'));
$table->addCell(1500, $styleCell)->addText('時間', $fontStyles, array( 'align' => 'center'));
foreach($result->result_array() as $ros){
$table->addRow();
$table->addCell(1500,$styleCell)->addText($ros['author'],array('bold'=>false),array( 'align' => 'center' ));
$table->addCell(3300,$styleCell)->addText($ros['title']);
$table->addCell(3300,$styleCell)->addText(cutstr_html($ros['neirong']));
$table->addCell(1500,$styleCell)->addText(date('Y-m-d',$ros['time']),array('bold'=>false),array( 'cellMarginTop' => 80));
$i++;
}
// $time = time();
// $time = date('Y年m月d日',$time);
$table->addRow(100);
$table->addCell(1500)->addText('', $fontStyles);
$table->addCell(3300)->addText('', $fontStyles);
$table->addCell(3300)->addText('', $fontStyles);
// $time = $time . ".doc";
$table->addCell(1500)->addText('共'.$i.'條', array( 'size' => 12 ) , array( 'align' => 'right'));
$time = time();
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save("$time.docx");