在laravel中使用PHPWord导出到docx时传递动态值

我正在尝试将用户详细信息导出到 .docx </ strong>文件。 该文件已成功导出,但它正在提供该对象。 Phpword格式不起作用。 如何编写代码以正确导出动态值,有人会帮助我吗?</ p>

AdminController.php </ strong> - </ p>

< pre> public function exportUserToDoc(Request $ request,$ id)
{
$ wordTest = new \ PhpOffice \ PhpWord \ PhpWord();
$ newSection = $ wordTest-&gt; addSection();

$ desc1 = User :: find($ id);

$ newSection-&gt; addText($ desc1,array('name'=&gt; $ desc1-&gt; name,'e​​mail'=&gt; $ desc1-&gt;电子邮件,'电话'=&gt; $ desc1-&gt;电话,'地址'=&gt; $ desc1-&gt;地址));
$ objectWriter = \ PhpOffice \ PhpWord \ IOFactory :: createWriter($ wordTest,'Word2007');
尝试{
$ objectWriter-&gt; save(storage_path('TestWordFile.docx'));
} catch(异常$ e){

}
返回响应( ) - &gt; download(storage_path('TestWordFile.docx'));
}
</ code> </ pre>

下载doc文件后,结果如下 - </ p>

  {“id”:1,“name”:“Emdadul Huq Shikder”,“email”:“emdadulshikder@gmail.com”,“phone”:“+ 8801674338411”,“地址 “  :“59 \ / 6 \ / 1 West Razabazar,Dhaka”,“status”:0,“created_at”:“2018-03-13 05:18:32”,“updated_at”:“2018-03-13 05:  35:51“} 
</ code> </ pre>

我希望得到格式化的结果,例如标题”姓名“,”电子邮件“等。</ p>
</ DIV>

展开原文

原文

I am trying to export user details to .docx file. The file successfully exported but, it's giving the object. Phpword format is not working. How to write code to export the dynamic values properly, would someone help me, please?

In 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'));
}

After downloading the doc file the result is like -

{"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"}

I want to get the result as well formatted such as with header 'Name', 'Email' etc.

duanhuang4306
duanhuang4306 你想要实现什么?应该如何格式化?一种解决方案是eMM提出的解决方案(使用TemplateProcessor),另一种解决方案是按原样添加文本,但是在添加文本之前应该格式化文本。查看samples/Sample_04_Textrun.php文件以获取示例
2 年多之前 回复
duancong7358
duancong7358 在phpword版本0.13.0中:addText(字符串$text,混合$fStyle=null,混合$pStyle=null)即你需要将格式化的文本字符串作为第一个参数传递给addText函数。
2 年多之前 回复

1个回答

Step 1: Create a .docx file using a word processing program(OpenOffice, LibreOffice Writer, MS Word etc.) which will serve as the template of your 'User Document'(this way you can set size, margins, orientation and other attributes and also format your document creatively using the tools provided in the word processing program you use).

For dynamic values in your document, you can use variables as placeholders. The syntax of a document variable is ${variable}. Make sure that your variable names are unique.

Here's a screenshot of an example document template with document variables. enter image description here

Step 2: Upload your document template to your server's storage path.

Step 3: Create a new TemplateProcessor and assign values to the variables you placed in your document template.

$desc1 = User::find($id);   

$my_template = new \PhpOffice\PhpWord\TemplateProcessor(storage_path('user_template.docx'));

$my_template->setValue('name', $desc1->name);
$my_template->setValue('email', $desc1->email);
$my_template->setValue('phone', $desc1->phone);
$my_template->setValue('address', $desc1->address); 

Step 4: Generate a safe file name(user_1.docx i.e.)

Step 5: Create a .docx file from your template with the safe file name you generated in the previous step.

try{
    $my_template->saveAs(storage_path('user_1.docx'));
}catch (Exception $e){
    //handle exception
}

Step 6: The complete code snippet to download the saved file.

public function exportUserToDoc(Request $request, $id)
{
    $desc1 = User::find($id);

    $my_template = new \PhpOffice\PhpWord\TemplateProcessor(storage_path('user_template.docx'));

    $my_template->setValue('name', $desc1->name);
    $my_template->setValue('email', $desc1->email);
    $my_template->setValue('phone', $desc1->phone);
    $my_template->setValue('address', $desc1->address);      

    try{
        $my_template->saveAs(storage_path('user_1.docx'));
    }catch (Exception $e){
        //handle exception
    }

    return response()->download(storage_path('user_1.docx'));
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐