PHPword在模板中插入表

时间:2013-03-21 01:41:22

标签: phpword

$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文件时如何插入表格 我可以在任何地方将模板中的表格插入吗?

3 个答案:

答案 0 :(得分: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');

答案 1 :(得分:2)

坏消息是:这不起作用。

setValue函数要求参数为纯文本,而$table包含对象。目前没有解决方案让$table对象返回可以与setValue一起使用的内容。

由于我遇到同样的问题,我已经创作了一个解决方案。要使其正常工作,您需要在模板中添加要在文档中使用的表。在此表中创建一个单行,每个单元格中都有一个变量名称。

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

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

答案 2 :(得分:1)

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

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