PHPword insert table in template

家住魔仙堡 提交于 2019-12-04 01:36:37

问题


$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$table = $section->addTable();
$i = 1;

$document = $PHPWord->loadTemplate('/var/sitLims/web/uploads/lmsSyllabus/lmsSyllabus.docx');
$document->setValue('Value1', $course_number);
$document->setValue('Value2', $course_name);
$document->setValue('Value3', $course_en_name);
$document->setValue('Value4', $course_summary);
$document->setValue('Value5', $course_purposes);
$document->setValue('Value6', $course_content);
$document->setValue('Value7', $course_exam);
$document->setValue('Value8', $course_description);
$document->setValue('Value9', $syllabus_person);
$document->setValue('Value10', $syllabus_academy_opinion);
foreach($syllabus_experiment as $a) {
$table->addRow();
$table->addCell(30)->addText($i);
$table->addCell(118)->addText($a->lmsExperiment->getExperimentName());
$table->addCell(118)->addText('');
$table->addCell(50)->addText($a->lmsExperiment->getExperimentExperimental());
    $table->addCell(50)->addText($a->lmsExperiment->getThisExperimentHours());
    $table->addCell(50)->addText($a->lmsExperiment->getExperimentEachNumber());
    $table->addCell(50)->addText($a->lmsExperiment->getExperimentLab());
    $table->addCell(50)->addText($a->lmsExperiment->getExperimentProjectArrange());
    $i ++;
}
$document->save('/var/sitLims/web/uploads/lmsSyllabus/' . $syllabus_name . '.docx');`

I am not sure how to insert the table when creating a word file Shall i insert the table in the template anywhere?


回答1:


Add it to PHPWord\Writer\Word2007\Document.php

public function getObjectAsText($element){

    if($this->getParentWriter()->getUseDiskCaching()) {
        $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
    } else {
        $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY);
    }
    if($element instanceof PHPWord_Section_Text) {
        $this->_writeText($objWriter, $element);
    } elseif($element instanceof PHPWord_Section_TextRun) {
        $this->_writeTextRun($objWriter, $element);
    } elseif($element instanceof PHPWord_Section_Link) {
        $this->_writeLink($objWriter, $element);
    } elseif($element instanceof PHPWord_Section_Title) {
        $this->_writeTitle($objWriter, $element);
    } elseif($element instanceof PHPWord_Section_TextBreak) {
        $this->_writeTextBreak($objWriter);
    } elseif($element instanceof PHPWord_Section_PageBreak) {
        $this->_writePageBreak($objWriter);
    } elseif($element instanceof PHPWord_Section_Table) {
        $this->_writeTable($objWriter, $element);
    } elseif($element instanceof PHPWord_Section_ListItem) {
        $this->_writeListItem($objWriter, $element);
    } elseif($element instanceof PHPWord_Section_Image ||
             $element instanceof PHPWord_Section_MemoryImage) {
        $this->_writeImage($objWriter, $element);
    } elseif($element instanceof PHPWord_Section_Object) {
        $this->_writeObject($objWriter, $element);
    } elseif($element instanceof PHPWord_TOC) {
        $this->_writeTOC($objWriter);
    }
    return trim(preg_replace("/[\x1-\x8\xB-\xC\xE-\x1F-\t+]/", "", $objWriter->getData()));

}

Insert table:

$obPHPWord = new PHPWord();
$obPHPWordDocument = $obPHPWord->loadTemplate('template.docx');
$section = $obPHPWord->createSection();
$table = $section->addTable();
$table->addRow(900);
// Add cells
$table->addCell(2000)->addText('Col 1');
$table->addCell(2000)->addText('Col 2');
$table->addCell(2000)->addText('Col 3');

$objWriter = PHPWord_IOFactory::createWriter($obPHPWord, 'Word2007');
$sTableText = $objWriter->getWriterPart('document')->getObjectAsText($table);
$obPHPWordDocument->setValue('myTable', $sTableText);
$obPHPWordDocument->save('result.docx');



回答2:


The bad news is: this won't work.

The setValue function expects the parameter to be plain text, while $table contains an object. Currently there's no solution to have the $table object return something that can be used with setValue.

As I had the same issue, I've created a work around. To get this working you've to add the table you would like to use in your document inside your template. Create one single row inside this table with a variable name inside each cell.

Call the cloneRow function from the patched version of PHPWord to receive the required number of table rows. After that just use setValue appending the row number to each variable.

You can get the code and full explanation on my site: http://jeroen.is/phpword-templates-with-repeating-rows/




回答3:


You can use it: https://github.com/Arisse/PHPWord_CloneRow

It's new version of CloneRow() and setValue(). Stable and easy to use.



来源:https://stackoverflow.com/questions/15538007/phpword-insert-table-in-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!