**
** 安装 使用
github下载地址:https://github.com/PHPOffice/PHPWord
也可直接使用composer安装,
composer require phpoffice/phpword你需要的controller用命名空间的方式引入
use PhpOffice\PhpWord\IOFactory; use PhpOffice\PhpWord\PhpWord;在controller中新建方法,写入下方代码
$PHPWord = new PhpWord(); // New portrait section $section = $PHPWord->createSection(); $arr['project_name'] = '云桥'; $arr['buy_start_time'] = '20180515'; $arr['buy_end_time'] = '20190825'; $arr['start_s'] = '20190825'; $arr['end_s'] = '20190825'; $arr['total'] = 25; $arr['tfee'] = 2500; // Add text elements $str = " ".$arr['project_name']."项目,与腾讯房产于".$arr['buy_start_time']."至".$arr['buy_end_time']."开展腾讯电商团购合作,".$arr['start_s']."至".$arr['end_s']."内,共计售出房屋". $arr['total']."套,成交明细见附件,收取服务费合计".$arr['tfee']."元,特此证明。"; $str5 = " 本确认函中的房源均已通过网签为确认标准,经双方授权代表签字后生效作为收款确认依据。且一旦签字盖章,乙方将不再承担该房源后续的电商团购费的退款责任。"; $str1 = "甲 方: 乙 方:"; $str2 = "授权代表签字: 授权代表签字:"; $str3 = "盖章: 盖章:"; $str4 = "签约时间:20 年 月 日 签约时间:20 年 月 日"; $title = '<h1>腾讯电商合作成交签约确认函</h1>'; $section->addText($title,'rStyle','pStyle'); $section->addTextBreak(2); $section->addText($str,'cOntent'); $section->addTextBreak(2); $section->addText($str5,'cOntent'); $section->addTextBreak(2); // $section->addText(iconv('utf-8','GB2312//IGNORE',$str1),'cOntent'); // $section->addText(iconv('utf-8','GB2312//IGNORE',$str2),'cOntent'); // $section->addText(iconv('utf-8','GB2312//IGNORE',$str3),'cOntent'); // $section->addText(iconv('utf-8','GB2312//IGNORE',$str4),'cOntent'); $section->addText($str1,'cOntent'); $section->addText($str2,'cOntent'); $section->addText($str3,'cOntent'); $section->addText($str4,'cOntent'); $section->addTextBreak(2); // $section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699')); $section->addTextBreak(2); $PHPWord->addFontStyle('cOntent', array('bold'=>false, 'size'=>12)); $PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>false, 'size'=>16,'align'=>'center')); $PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100)); // $section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle'); // $section->addText('I have only a paragraph style definition.', null, 'pStyle'); // Save File $objWriter = IOFactory::createWriter($PHPWord, 'Word2007'); /*header("Content-Type: application/doc"); header("Content-Disposition: attachment; filename=".date("YmdHis").".doc");*/ $path = 'wordfile/'.date("YmdHis").'.doc'; $objWriter->save($path); $file1 = fopen($path, "r"); // 输入文件标签 Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); Header("Accept-Length:".filesize($path)); Header("Content-Disposition: attachment;filename=" . date("YmdHis").'.doc'); ob_clean(); // 重点!!! flush(); // 重点!!!!可以清除文件中多余的路径名以及解决乱码的问题: //输出文件内容 //读取文件内容并直接输出到浏览器 echo fread($file1, filesize($path)); fclose($file1); exit();