【发布时间】:2016-12-23 06:24:05
【问题描述】:
我需要根据用户输入替换一些 word 文档中的内容。我正在尝试读取模板文件(例如“template.docx”),并替换名字 {fname}、地址 {address} 等。
模板.docx:
To,
The Office,
{officeaddress}
Sub: Authorization Letter
Sir / Madam,
I/We hereby authorize to {Ename} whose signature is attested here below, to submit application and collect Residential permit for {name}
Kindly allow him to support our International assignee
{name} {Ename}
有没有办法在 Laravel 5.3 中做同样的事情?
我正在尝试使用 phpword,但我只能看到编写新 word 文件的代码 - 但不能读取和替换现有文件。另外,当我简单地读写时,格式就搞砸了。
代码:
$file = public_path('template.docx');
$phpWord = \PhpOffice\PhpWord\IOFactory::load($file);
$phpWord->save('b.docx');
b.docx
To,
The Office,
{officeaddress}
Sub:
Authorization Letter
Sir / Madam,
I/We hereby authorize
to
{Ename}
whose signature is attested here below, to submit a
pplication and collect Residential permit
for
{name}
Kindly allow him to support our International assignee
{name}
{
E
name}
【问题讨论】:
-
这里是从 .docx 文件中读取的示例:github.com/PHPOffice/PHPWord/blob/develop/samples/…
-
谢谢大家 - 我会试试看。
标签: php laravel ms-word laravel-5.3 docx