This project is read-only.

How to write Chinese charater with PHPWord

Oct 19, 2010 at 1:44 PM
<?php
require_once '../PHPWord.php';

$filename = date('Y-m-d-H-i-s',time()).'.doc';			//filename

$PHPWord  = new PHPWord();
$document = $PHPWord->loadTemplate('Template.docx');


$programme_name=iconv("GB2312","UTF-8","中国");


$document->setValue('Value1', $programme_name);
$document->setValue('Value2', 'Benjing');
$document->setValue('Value3', 'Venus');
$document->setValue('Value4', 'Earth');
$document->setValue('Value5', 'Mars');
$document->setValue('Value6', 'Jupiter');
$document->setValue('Value7', 'Saturn');
$document->setValue('Value8', 'Uranus');
$document->setValue('Value9', 'Neptun');
$document->setValue('Value10', 'Pluto');

$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));

$document->save($filename);
?>
it can not write Chinese charater into the .docx  file
can some body tell me 
how to solve this problem
I have fix the code with this way  http://phpword.codeplex.com/workitem/8 
but it still can not work
May 4, 2011 at 12:38 AM

did you found a way ? i looking all so for this issue ... thanks

May 14, 2011 at 5:21 PM
Edited May 14, 2011 at 5:21 PM

hello

add :

header('Content-type: text/html; charset=UTF-8');

and remove all the utf-8 info in the php files... it work fine for me in arabic.

May 16, 2011 at 6:38 AM

我也遇到了这个问题,求解。

May 16, 2011 at 7:36 AM
Edited May 16, 2011 at 7:36 AM
bg6aer wrote:
<?php
require_once '../PHPWord.php';

$filename = date('Y-m-d-H-i-s',time()).'.doc';			//filename

$PHPWord  = new PHPWord();
$document = $PHPWord->loadTemplate('Template.docx');


$programme_name=iconv("GB2312","UTF-8","中国");


$document->setValue('Value1', $programme_name);
$document->setValue('Value2', 'Benjing');
$document->setValue('Value3', 'Venus');
$document->setValue('Value4', 'Earth');
$document->setValue('Value5', 'Mars');
$document->setValue('Value6', 'Jupiter');
$document->setValue('Value7', 'Saturn');
$document->setValue('Value8', 'Uranus');
$document->setValue('Value9', 'Neptun');
$document->setValue('Value10', 'Pluto');

$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));

$document->save($filename);
?>
it can not write Chinese charater into the .docx  file
can some body tell me 
how to solve this problem
I have fix the code with this way  http://phpword.codeplex.com/workitem/8 
but it still can not work

解决方法:http://fav100.info/archives/PHPWord-Chinese-charater/

找到 setValue方法,把里面 的 $replace = utf8_encode($replace); 换成 iconv('gbk','utf-8',$replace); 即可。如果需要还可以修改此方法,以便根据不同的需要设置编码。

public function setValue($search, $replace) {
        if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
            $search = '${'.$search.'}';
        }
 
        if(!is_array($replace)) {
           // $replace = utf8_encode($replace);
           $replace =iconv('gbk', 'utf-8', $replace);
        }