我正在尝试将用户详细信息导出到.docx文件。该文件已成功导出,但它正在提供对象。phpword 格式不工作。如何编写代码以正确导出动态值,有人可以帮助我吗?
在AdminController.php -
public function exportUserToDoc(Request $request, $id)
{
$wordTest = new \PhpOffice\PhpWord\PhpWord();
$newSection = $wordTest->addSection();
$desc1 = User::find($id);
$newSection->addText($desc1, array('name' => $desc1->name, 'email' => $desc1->email, 'phone' => $desc1->phone, 'address' => $desc1->address));
$objectWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordTest, 'Word2007');
try{
$objectWriter->save(storage_path('TestWordFile.docx'));
}catch (Exception $e){
}
return response()->download(storage_path('TestWordFile.docx'));
}
下载 doc 文件后,结果如下 -
{"id":1,"name":"Emdadul Huq Shikder","email":"emdadulshikder@gmail.com","phone":"+8801674338411","address":"59\/6\/1 West Razabazar, Dhaka","status":0,"created_at":"2018-03-13 05:18:32","updated_at":"2018-03-13 05:35:51"}
我想得到格式正确的结果,例如标题“名称”、“电子邮件”等。