/引入wordphp核心文件 require './ThinkPHP/Library/Vendor/PHPWord/PHPWord.php';
require './ThinkPHP/Library/Vendor/PHPWord/PHPWord/IOFactory.php';
// New Word Document
$PHPWord = new \PHPWord();
// New portrait section
$section = $PHPWord->createSection();
// 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 = iconv('utf-8','GB2312//IGNORE','腾讯电商合作成交签约确认函');
$section->addText($title,'rStyle','pStyle');
$section->addTextBreak(2);
$section->addText(iconv('utf-8','GB2312//IGNORE',$str),'cOntent');
$section->addTextBreak(2);
$section->addText(iconv('utf-8','GB2312//IGNORE',$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->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 = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('./Public/电商成交确认函'.date("YmdHis").'.docx');
echo './Public/电商成交确认函'.date("YmdHis").'.docx'; //返回生成的word文件的路径
}//将生成的文件用次函数进行下载
public function download_file(){
$file = $_GET['url'];
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename=电商成交确认函.docx');
header("Content-Length: ". filesize($file));
readfile($file);
}参考链接
https://github.com/PHPOffice/PHPWord#installation
https://phpword.readthedocs.io/en/latest/
http://www.yiichina.com/tutorial/785
https://blog.csdn.net/u012169212/article/details/52718290
https://blog.csdn.net/ning521513/article/details/55257958
https://blog.csdn.net/leejianjun/article/details/52232816
