当前位置: 移动技术网 > IT编程>开发语言>PHP > phpword插件导出word文件时中文乱码问题处理方案

phpword插件导出word文件时中文乱码问题处理方案

2018年07月21日  | 移动技术网IT编程  | 我要评论

91今金贷,rania组合,全宇蓝

最近一个项目开发要用到php技术导出word文档,比较了几种方案,首先是使用microsoft office自带的activex/com组件,比如word.application,这种方式的优点是格式兼容度高,可以生成纯doc的word2003格式文档,缺点一是比较占资源(调用会启动一个winword.exe进程),不适合web多用户访问使用;二是php这种web开发技术大多数是跑在linux服务器上,当然也就无法使用windows下的技术了,平台可移植和兼容性不好。

第二种生成word的方案是生成word兼容的网页格式,然后以word方式打开,这种方案总体上感觉怪怪的,毕竟文件格式是html的,而且格式兼容度不好,不过这种方式的优点是节省服务器资源,能够快速生成;最后一种方案也就是今天的主角,采用phpword生成word2007(docx)格式的文档,现在基本上微软office word 2003以后的版本均兼容这种格式了,对于2003版本来说,仅需要下载安装个兼容格式包(下载地址),也能正常打开这类文件,当然如果你使用的是最新版本的office(包括但不限于office 2007、office 2010)则不需要安装此格式包。

好了,下面我就介绍一下phpword,大家可以通过访问项目主页下载并获得关于项目的更多信息。

我在使用过程中主要遇到了中文乱码的问题,结合网上大神们的指导,通过下面的方式解决了这类问题,希望对大家有所帮助。

1、增加东亚字体支持 

打开并编辑路径/writer/word2007/base.php文件内容,大概在第349行(行数随着版本可能会有变化)大概函数_writetextstyle内添加:

$objwriter->writeattribute('w:eastasia', $font)
比如我的修改片段基本是下面这样:

// 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();
}

2. 解决中文乱码问题

编辑phpword/template.php,找到代码$replace = utf8_encode($replace);,删除或者注释掉这行代码,添加$replace = iconv( 'gbk','utf-8', $replace);,比如代码改为如下:

 /**
 * set a template value
 * 
 * @param mixed $search
 * @param mixed $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); // 注释掉上面行后添加这行
  }
 
  $this->_documentxml = str_replace($search, $replace, $this->_documentxml);
}


调用方式如下:

 

$document->setvalue('template', iconv('utf-8', 'gb2312//ignore', '中文'));

上面的代码主要解决模板的问题,下面同样的道理,解决section添加文本的问题,找到代码$giventext = utf8_encode($text);,删除或者注释掉这行代码,添加$giventext = iconv('gbk', 'utf-8', $text);,比如代码如下:

 

/**
 * add a text element
 * 
 * @param string $text
 * @param mixed $stylefont
 * @param mixed $styleparagraph
 * @return phpword_section_text
 */
public function addtext($text, $stylefont = null, $styleparagraph = null) {
  //$giventext = utf8_encode($text);
  $giventext = iconv('gbk', 'utf-8', $text); // 注释掉上面行后添加这行
  $text = new phpword_section_text($giventext, $stylefont, $styleparagraph);
  $this->_elementcollection[] = $text;
  return $text;
}

调用方式和上面的模板调用大同小异,这边就不列举了。

折腾了这么多,突然发现网上还有另外一个版本的phpword,项目类名大小写上略有不同,隶属于phpoffice/phpword,github项目地址(文档)。这个版本的phpword内容更加丰富,支持的功能也比较多(包括行间距,缩进和首行缩进等),最后我也采取的这个版本的phpword,值得注意的是这两个版本的phpword在api接口上基本一致,可以通用。但是有些api,在phpoffice/phpword里是不推荐的,比如createsection需要改成addsection,另外应用这个版本的phpword不需要像上面那样做任何中文支持的修改,比较省事。

这两个phpword项目的官方都提供了较详细的使用例子和文档,这里就不介绍了。最后提示的是:在模板模式下loadtemplate,只能使用setvalue等模板操作方法,不能再添加段落或者段落修改了。这个略有不便。

对于phpoffice/phpword我提供一个简单的例子供参考(当然官方例子更多):

require_once 'phpoffice/phpword/phpword.php'; // 包含头文件
use phpoffice\phpword\autoloader;
use phpoffice\phpword\settings;
use phpoffice\phpword\iofactory;
 
require_once __dir__ . '/phpoffice/phpword/autoloader.php';
autoloader::register();
settings::loadconfig();
 
// create a new phpword object
$phpword = new \phpoffice\phpword\phpword();
$phpwordhelper= new \phpoffice\phpword\shared\font();
 
$phpword->setdefaultfontname('仿宋'); // 全局字体
$phpword->setdefaultfontsize(16);   // 全局字号为3号
 
// 设置文档的属性,这些在对文档右击属性可以看到,也可以省去这些步骤
$properties = $phpword->getdocumentproperties();
$properties->setcreator('张三');  // 创建者
$properties->setcompany('某公司'); // 公司
$properties->settitle('某某文档'); // 标题
$properties->setdescription('http://wangye.org'); // 描述
$properties->setlastmodifiedby('李四'); // 最后修改
$properties->setcreated( time() );   // 创建时间
$properties->setmodified( time() );   // 修改时间
 
// 添加3号仿宋字体到'fangsong16pt'留着下面使用
$phpword->addfontstyle('fangsong16pt', array('name'=>'仿宋', 'size'=>16));
 
// 添加段落样式到'normal'以备下面使用
$phpword->addparagraphstyle(
 'normal',array(
  'align'=>'both',
  'spacebefore' => 0,
  'spaceafter' => 0,
  'spacing'=>$phpwordhelper->pointsizetotwips(2.8),
  'lineheight' => 1.19, // 行间距
  'indentation' => array( // 首行缩进
   'firstline' => $phpwordhelper->pointsizetotwips(32)
  )
 )
);
 
// section样式:上3.5厘米、下3.8厘米、左3厘米、右3厘米,页脚3厘米
// 注意这里厘米(centimeter)要转换为twips单位
$sectionstyle = array(
  'orientation' => null,
  'marginleft' => $phpwordhelper->centimetersizetotwips(3),
  'marginright' => $phpwordhelper->centimetersizetotwips(3),
  'margintop' => $phpwordhelper->centimetersizetotwips(3.5),
  'marginbottom' => $phpwordhelper->centimetersizetotwips(3.8),
  'pagenumberingstart' => 1, // 页码从1开始
  'footerheight' => $phpwordhelper->centimetersizetotwips(3),
);
 
$section = $phpword->addsection($sectionstyle); // 添加一节
 
// 下面这句是输入文档内容,注意这里用到了刚才我们添加的
// 字体样式fangsong16pt和段落样式normal
$section->addtext('文档内容', 'fangsong16pt', 'normal');
$section->addtextbreak(1); // 新起一个空白段落
 
$objwriter = iofactory::createwriter($phpword, 'word2007');
$objwriter->save('/path/to/file'); // 保存到/path/to/file路径下

总结

1、用模板word生成word中文乱码解决方案:打开phpword/template.php文件,找到$replace = utf8_encode($replace);将其改为$replace =iconv('gbk', 'utf-8', $replace); 即可。

2、直接生成word文档,调用addtext对象时中文乱码解决方案:打开phpword/section.php文件,找到$giventext = utf8_encode($text);将其改为$giventext = iconv('gbk', 'utf-8', $text);即可。

3、貌似其他方法也类似第解决。

4、注意php文件采用gbk哦。反正我的显示中文了。在网上找了好久,研究了半天才搞定。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网