尝试在 Laravel 上使用 PhpWord 有人可以帮忙吗?

问题描述

我第一次尝试使用 PHPWord 所以我写了这段代码但它不起作用我认为问题我不知道如何获取页面数据

所有链接都运行良好 我对如何获取诸如查询之类的计划数据感到困惑

我的代码

public function getDecumentReport(Request $request)
{

    // get the Page Data
    $datas = Decument::orderBy('id','ASC')->get();

    // Get the Word Document
    $PHPWord = new \PHPOffice\PHPWord\PHPWord();
    $document = $PHPWord->loadTemplate('doc/dec_plan.docx');

    $management = Management::find($data->management);
    if($management != null) {
        $management = $management->name;
    } else {
        $management = "";
    }

    $department = Department::find($data->department);
    if($department != null) {
        $department = $department->name;
    } else {
        $department = "";
    }

    $document->setValue('management',($management) );
    $document->setValue('department',($department) );
    //---Use This Part Inside Foreach and Use a Word Table
    if ($datas) {
        foreach ($datas AS $data) {
            $document->setValue('name',($datas->name));
            $document->setValue('describ',($data->describ));
            $document->setValue('boll',($data->boll));
            $document->setValue('type',($data->place));
            $document->setValue('date',($data->date));
            $document->setValue('unit',($data->unit));
            $document->setValue('stitaiom',($data->stitaiom));
        }
    }

    $name = 'Document' . time() . '.docx';
    $document->saveAs( "dec_plan/" . $name);
    $file = "dec_plan/{$name}";

    $headers = array(
        //'Content-Type: application/msword','Content-Type: vnd.openxmlformats-officedocument.wordprocessingml.document'
   );

    $response = Response::download($file,$name,$headers);
    return $response;
}

页面是: that Schedule I want to export to word

解决方法

在 PHPWord 中使用 XML:

<header id="header">
      <img src="https://png.pngtree.com/element_pic/16/05/30/11574bb301599cc.jpg" alt="" id="header-img">
      <nav id="nav-bar">
        <ul>
          <li><a href="#video" class="nav-link">Features</a></li>
          <li><a href="#video" class="nav-link">How it works</a></li>
          <li><a href="#video" class="nav-link">Pricing</a></li>
        </ul>
      </nav>
    </header>
    <div id="container">
      <div class="choices">
        <form action="">
          <input type="button" value="choose" class="pilih">
        </form>
      </div>
      <div class="choices">
        <form action="">
          <input type="button" value="choose" class="pilih">
        </form>
      </div>
      <div class="choices">
        <form action="">
          <input type="button" value="choose" class="pilih">
        </form>
      </div>
    </div>

首先创建一个word文件的模板并压缩你的word文件并获取word文件的xml,如下所示: create a rar file of your template

select .docx file and click view

open the word folder

copy the xml file

您可以复制您的 xml 文件并在以下站点中安排您的代码: https://codebeautify.org/xmlviewer

注意:记住只复制 xml 文件的表格部分,即“”标签内的代码,不要复制 xml 文件的多余部分。