一:安装phpword类库
composer require phpoffice/phpword
报错:(composer require 安装内存不足)
Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes)
再次安装:(临时分配无限内存安装即可)
php -d memory_limit=-1 composer require phpoffice/phpword
二:引入phpword类
use PhpOffice\PhpWord\PhpWord;
三:创建word文件
$phpWord = new PhpWord();
$phpWord->setDefaultFontName('黑体');//设置字体
$phpWord->setDefaultFontSize(13);//字号
$section = $phpWord->addSection();//创建空白页
$section->addTitle('这是标题');
$section->addTextBreak();//空白行
$section->addText('行内文本');
$writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');//生成文件
$files = 'test.docx';
$writer->save($files);
$name = basename($files);//文件名(test)
return response()->download($files, $name, $headers = ['Content-Type'=>'application/zip;charset=utf-8']);//下载文件