dsjlqkbpn029473708 2018-03-13 10:55
浏览 646

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

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.

  • 写回答

1条回答 默认 最新

  • dtujfmfs06058 2018-03-13 12:52
    关注

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

报告相同问题?

悬赏问题

  • ¥15 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?