1. 程式人生 > >PHPWord利用模板替換字串生成精確的word文件

PHPWord利用模板替換字串生成精確的word文件

phpword處理docx模板時候始終發生神奇的BUG,就是複製原版例子裡的${Value1}進自己的模板然後替換是沒問題的,但是隻要一改動這個變數文字,PHP做相應替換就失效了。

用了下殘廢百度無果,一怒翻起google,準確度高多了。

   原來有2個PHPWord專案:

其中適用比較廣的是PHPOffice專案下的一個子專案

git地址:https://github.com/PHPOffice/PHPWord

檢視官方的文件:

http://phpword.readthedocs.org/en/latest/templates-processing.html?highlight=replace

具體如何用模板做替換:

include_once ('exec/lib/phpword/src/PhpWord/PHPWord.php');
use PhpOffice\PhpWord\Autoloader;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\IOFactory;

include_once ('exec/lib/phpword/src/PhpWord/Autoloader.php');
Autoloader::register();
Settings::loadConfig();

// Create a new PHPWord Object
$PHPWord = new \PhpOffice\PhpWord\PhpWord();
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('statics/template/adminPositive.docx');
$templateProcessor->setValue('xm1', '姓名');
$templateProcessor->setValue('zw1', '公務員');
$templateProcessor->setValue('sfz1', '360281199909090009');
$templateProcessor->setValue('gz1', '統發');
//$templateProcessor->setValue('Street', 'Coming-Undone-Street 32');
$templateProcessor->saveAs('test.docx');

這裡要注意的是換行問題。

另外copy一篇比較nice的博文做個備份

轉載自:http://wangye.org/blog/archives/943/

===============================================================================================

最近一個專案開發要用到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/PHPWordGitHub專案地址文件)。這個版本的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路徑下