咨询电话:
15628812133
21
2017/04

PHPword 中文乱码,终极解决方案最新版+1

发布时间:2017-04-21 18:11:27
发布者:pengyifeng
浏览量:
0

111.png

1、解决编码问题,PHPword 会对输入的文字进行utf8_encode编码转化,如果你使用GBK、GB2312或者utf8编码的话就会出现乱码,如果你用utf8编码,就查找类库中所有方法中的 utf8_encode 转码将其删除,如果你采用GBK或者GB2312编码,使用iconv进行编码转换。2、解决中文字体支持,在writer/word2007/base.php中 312行添加 $objWriter->writeAttribute('w:eastAsia',$font)3、启动php zip支持,windows环境下在php配置文件php.ini中,将extension=php_zip.dll前面的分号“;”去除;(如果没有,请添加extension=php_zip.dll此行并确保php_zip.dll文件存在相应的目录),然后同样在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('标题',$fontStyles, array( 'align' => 'center'));

$table->addCell(3300, $styleCell)->addText('内容', $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");


关键词:
返回列表