search
HomeBackend DevelopmentPHP Tutorialphpword插件导出word文件时中文乱码问题处理方案_PHP

最近一个项目开发要用到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哦。反正我的显示中文了。在网上找了好久,研究了半天才搞定。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Give an example of how to store a user's name in a PHP session.Give an example of how to store a user's name in a PHP session.Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.