【问题标题】:how to fill in table on template file from phpword?如何从 phpword 填写模板文件的表格?
【发布时间】:2023-03-17 02:18:01
【问题描述】:

如何将数据库员工的表格插入到我的模板文档中? 我成功填写了公司字段。

下面的代码

require_once APPPATH.'libraries/autoload.php';
use PhpOffice\PhpWord\PhpWord;
public function cetaksurat()
{
   $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('source.docx');
   $templateProcessor->setValues([
        'company' => 'my company',
        //how to insert employee table from my database array result?
        //.....
   ]);
   header("Content-Disposition: attachment; filename=result.docx");
   $templateProcessor->saveAs('php://output');
}

谢谢

【问题讨论】:

    标签: php database codeigniter phpword


    【解决方案1】:

    在我的情况下,我会使用:

    $news2 = $this->phpword_model->get_employe(); //

            foreach($news2 as $key=>$values) {
        $array_values1 = $values['id'];
        $array_values2 = $values['name'];  
    }
    
    $templateProcessor->setValue('id', $array_values1);
    $templateProcessor->setValue('name', $array_values2);
    
    
        $templateProcessor->saveAs($filename);
    

    /// 第一个示例是针对单个记录的。 现在这个例子是针对多条记录的。

            $news7 = $this->phpword_model->get_employes();
            $templateProcessor->cloneRow('id', count($news7));
    
             $i=1;
    foreach($news7 as $key=>$values) {
        $templateProcessor->setValue('id#'.$i++, $values['id']); 
    }
    
                 $i2=1;
    foreach($news7 as $key=>$values) {
        $templateProcessor->setValue('name#'.$i2++, $values['name']);
    }
    
        $templateProcessor->saveAs($filename);
    

    【讨论】:

      猜你喜欢
      • 2019-06-13
      • 2013-03-10
      • 1970-01-01
      • 2018-03-05
      • 2015-10-17
      • 2014-03-03
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      相关资源
      最近更新 更多