Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add table into template #2014

Closed
NielDuysters opened this issue Feb 9, 2021 · 2 comments
Closed

Add table into template #2014

NielDuysters opened this issue Feb 9, 2021 · 2 comments

Comments

@NielDuysters
Copy link

I have a template .docx file in which I want to place a table. I can already read-in the template file and search-and-replace simple strings in the template.

But now I want to put a table in the template.

This is my code:

$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($template);
            $phpWord = new \PhpOffice\PhpWord\PhpWord();

            $templateProcessor->setValue('customer_name', $name); 
            $templateProcessor->setValue('customer_address', $address); 
            $templateProcessor->setValue('customer_city', $city); 
            $templateProcessor->setValue('customer_name', $name); 
            $templateProcessor->setValue('invoice_date', $date); 

            $table_section = $phpWord->addSection();
            $rows = 10;
            $cols = 5;
            $table_section->addText('Basic table', "rofl");

            $table = $table_section->addTable();
            for ($r = 1; $r <= $rows; $r++) {
                $table->addRow();
                for ($c = 1; $c <= $cols; $c++) {
                    $table->addCell(1750)->addText("Row {$r}, Cell {$c}");
                }
}

            $objWriter = new PhpOffice\PhpWord\Writer\Word2007($phpWord);
            $tableStr = $objWriter->getWriterPart('Document')->getTableAsText($table);

            $templateProcessor->setValue('product_table', $tableStr); 


            $templateProcessor->saveAs($new_file_path);

I have added this:

/* CUSTOM FUNCTION */
    function getTableAsText($element) {
        $xmlWriter = $this->getXmlWriter();
        $writer = new \PhpOffice\PhpWord\Writer\Word2007\Element\Table($xmlWriter, $element);
        $writer->write();
        return $xmlWriter->getData();
    }

to PhpWord/Writer/Word2007/Part/Document.php.

As output I just get all the rows as a paragraph, not at all as a table...

@troosan
Copy link
Contributor

troosan commented Feb 13, 2021

De comment here #2015

@troosan troosan closed this as completed Feb 13, 2021
@ticelso
Copy link

ticelso commented Dec 29, 2023

Hello good morning.
Happy New Year to everyone there.
I am trying to implement the table in my template and I am following your tutorial.
It turns out that I received the following error message:
Fatal error: Uncaught Error: Call to undefined method PhpOffice\PhpWord\Writer\Word2007\Part\Document::getTableAsText() in
Is the function deprecated?
Thank you.

Versão em português:
Olá bom dia.
Feliz ano novo para todos ai.
Estou tentanto inplementar a tabela no meu template e estou seguindo seu tutorial.
Ocorre que recebi a seguinte mensagem de erro:
Fatal error: Uncaught Error: Call to undefined method PhpOffice\PhpWord\Writer\Word2007\Part\Document::getTableAsText() in
Está depreciada a função?
Obrigado.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants