phpword模板处理-生成word

PHP使用phpword替换模板内容,不多说,直接上代码 /** * @param $param * @throws \PhpOffice\PhpWord\Exception\CopyFileException * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException * 生成教学word */ public function testDataToWord($param){ $data = json_decode(ExcelTo

PHP使用phpword替换模板内容,不多说,直接上代码
/**
     * @param $param
     * @throws \PhpOffice\PhpWord\Exception\CopyFileException
     * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
     * 生成教学word
     */
    public function testDataToWord($param){
        $data = json_decode(ExcelToWordService::$data,true);//数据
        $document = new TemplateProcessor('/home/work/XXXX/Lesson_plan_module_10_1_3.docx');//模板地址
        Settings::setOutputEscapingEnabled(true);//输出转义
        $document->setValue('course_theme', $data[3][2]);
        $document->setValue('course_time', $data[5][2]);
        $document->setValue('course_target', $data[6][2]);
        $document->setValue('course_focus', $data[7][2]);
        $document->saveAs('./target.docx');
        ob_clean();
        ob_start();
        $fp = fopen('./target.docx',"r");
        $file_size = filesize('./target.docx');
        Header("Content-type:application/octet-stream");
        Header("Accept-Ranges:bytes");
        Header("Accept-Length:".$file_size);
        Header("Content-Disposition:attchment; filename=".'XXXX.docx');//生成的word名称
        $buffer = 1024;
        $file_count = 0;
        while (!feof($fp) && $file_count < $file_size){
            $file_con = fread($fp,$buffer);
            $file_count += $buffer;
            echo $file_con;
        }
        fclose($fp);
        ob_end_flush();
    }
问题1,在替换中会遇到转义字符输出错乱的情况,加上如下代码就OK了……
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
 

本文标题为:phpword模板处理-生成word

基础教程推荐