PHPword insert table in template

xiaobo picture xiaobo · Mar 21, 2013 · Viewed 21.2k times · Source
$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?

Answer

user3686423 picture user3686423 · May 29, 2014

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');