4
$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');`

创建word文件时我不确定如何插入表格我应该在模板中的任何地方插入表格吗?

4

3 回答 3

3

将其添加到 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()));

}

插入表:

$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');
于 2014-05-29T06:37:29.687 回答
2

坏消息是:这行不通。

setValue函数期望参数是纯文本,同时$table包含一个对象。目前没有解决方案让$table对象返回可以与setValue.

因为我遇到了同样的问题,所以我创建了一个解决方法。要使其正常工作,您必须在模板内的文档中添加要在文档中使用的表格。在此表内创建一行,每个单元格内都有一个变量名。

从 PHPWord 的修补版本调用该cloneRow函数以接收所需的表行数。之后,只需setValue将行号附加到每个变量。

您可以在我的网站上获取代码和完整说明:http: //jeroen.is/phpword-templates-with-repeating-rows/

于 2013-04-04T20:18:50.180 回答
2

你可以使用它:https ://github.com/Arisse/PHPWord_CloneRow

这是CloneRow()setValue()的新版本。稳定且易于使用。

于 2014-03-05T13:31:59.933 回答