奇乐网 - 草根博客网站

发一个office模板替换类(虎绿林)

admin 216 2

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

脱胎于phpoffice,主要解决了模板替换时受xml样式干扰问题,解决了中文乱码问题。解决换行问题。注意php编码需要为utf-8
源码很简单,有需求的可以下载尝试。
源代码:

<?php
/**
 * PHPWord
 *
 * Copyright (c) 2011 PHPWord
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPWord
 * @package    PHPWord
 * @copyright  Copyright (c) 010 PHPWord
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version    Beta 0.6.3, 08.07.2011
 */


/**
 * PHPWord_DocumentProperties
 *
 * @category   PHPWord
 * @package    PHPWord
 * @copyright  Copyright (c) 2009 - 2011 PHPWord (http://www.codeplex.com/PHPWord)
 */
class PHPWord_Template {
    
    /**
     * ZipArchive
     * 
     * @var ZipArchive
     */
    private $_objZip;
    
    /**
     * Temporary Filename
     * 
     * @var string
     */
    private $_tempFileName;
    
    /**
     * Document XML
     * 
     * @var string
     */
    private $_documentXML;
    private $_matches;
    
    /**
     * Create a new Template Object
     * 
     * @param string $strFilename
     */
    public function __construct($strFilename) {
        $path = dirname($strFilename);
        $this->_tempFileName = $path.DIRECTORY_SEPARATOR.time().'.docx';
        
        copy($strFilename, $this->_tempFileName); // Copy the source File to the temp File

        $this->_objZip = new ZipArchive();
        $this->_objZip->open($this->_tempFileName);
        
        $this->_documentXML = $this->_objZip->getFromName('word/document.xml');
        preg_match_all('/\$[^{]*\{[^}]+\}/s', $this->_documentXML, $matches);
        $this->_matches=array_unique($matches[0]);
        unset($matches);
    }
    
    /**
     * Set a Template value
     * 
     * @param mixed $search
     * @param mixed $replace
     */
public function setValue($search, $replace, $limit=-1) {
    if(substr($search, 0, 1) !== '{' && substr($search, -1) !== '}') {
        $search = '${'.$search.'}';
    }
    
    if(!is_array($replace)) {
        // $replace = utf8_encode($replace);
        // $replace = iconv( 'gbk','utf-8', $replace);
        $replace = str_replace("\n","<w:br />",$replace);
    }

    
    $arrayy=$this->_matches;
    foreach ($arrayy as $k => $match) {
        $no_tag = strip_tags($match);
        if ($no_tag == $search) {
            //$match = '{'.$match.'}';
            $this->_documentXML = str_replace($match, $replace, $this->_documentXML);   
        }
    }
unset($arrayy);
}
    /**
     * Save Template
     * 
     * @param string $strFilename
     */
    public function save($strFilename) {
        if(file_exists($strFilename)) {
            unlink($strFilename);
        }
        
        $this->_objZip->addFromString('word/document.xml', $this->_documentXML);
        
        // Close zip file
        if($this->_objZip->close() === false) {
            throw new Exception('Could not close zip file.');
        }
        
        rename($this->_tempFileName, $strFilename);
    }
}
?>

实例代码:

$phpword= new PHPWord_Template("1.docx");//加载模板文件1.docx
$phpword->setValue("tq1","替换文本");//tq1在docx文件里面表示为${tq1}
$phpword->save("2.docx");//保存为2docx

file-hash-png-d7a10e4c832a4b8f418df1db7b293b1745441.png

评论列表 (已有2条评论)

    vultr优惠码
    2019-05-27 14:10

    收藏了

      admin
      2019-05-27 21:04

      谢谢支持

发表评论 (已有2条评论)