4

我在我的 Laravel 应用程序中使用 PHPOffice/PHPWord。它用于生成带有表格结果的 .docx 文档。这对于包含 3 个 6 行的表格的文档非常有用,但是当有更多行时,会生成文档,但是在打开它时会发生以下错误:

We're sorry, We can't open (documentname) because we found a problem with its contents.

Details: XML parsing error Location: Part:/word/document.xml, Line: 2, Column 14349. 

现在,我已经开始在另一个结果页面上工作,我还想在其中生成一个 .docx 文档。这将包含 5 个表,但有 3 行我得到相同的 XML 解析错误,但在不同的位置(位置:部分:/word/document.xml,行:4,列:2888)。有人可以向我解释这是我的代码中的错误,还是 phpword/words 中的错误?

我通过删除所有内容并慢慢添加新行来完成一些故障排除。我发现了错误,但我该如何解决。前两个表生成良好..

    $phpWord = new \PhpOffice\PhpWord\PhpWord();

    $section = $phpWord->addSection();
    $section->addImage('../public/img/2.jpg', array('width' => 230, 'height' => 65, 'alignment' => 'left'));
    $section->addText('Project IDs:' . $parameter);
    $header =$section->addHeader();
    $header->addText('Results Summary');

    $section->addLine(
        array(
            'width'       => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16),
            'height'      => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0),
            'positioning' => 'absolute',
        )
    );
    $tableName = 'rStyle';
    $phpWord->addFontStyle($tableName, array('italic' => true, 'size' => 12));
    $thName = 'tStyle';
    $phpWord->addFontStyle($thName, array('bold' => true, 'size' => 9));

    $section->addText('General Information Table', $tableName);
    $fancyTableStyle = array('borderSize' => 6, 'borderColor' => '999999');
    $spanTableStyleName = 'Overview tables';
    $phpWord->addTableStyle($spanTableStyleName, $fancyTableStyle);
    $table = $section->addTable($spanTableStyleName);
    $table->addRow(null, array('tblHeader' => true, 'cantSplit' => true));
    $table->addCell(1750)->addText('Project ID',$thName);
    $table->addCell(1750)->addText('Description',$thName);
    $table->addCell(1750)->addText('Notes',$thName);
    foreach ($id_array_explode as $char) {
        $table->addRow();
        $singlenumber = (int)$char;
        $cursor = $collection->find(array("id" => $singlenumber));
        foreach ($cursor as $document) {
                    $table->addCell(1750)->addText($document["project_id"]);
                    $table->addCell(1750)->addText($document["description"]);
                    $table->addCell(1750)->addText($document["notes"]);
        }
    }
    $section->addText('        
');
    $section->addLine(
        array(
            'width'       => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16),
            'height'      => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0),
            'positioning' => 'absolute',
        )
    );
    $section->addText('Input Table', $tableName);
    $table1 = $section->addTable($spanTableStyleName);
    $table1->addRow(null, array('tblHeader' => true, 'cantSplit' => true));
    $table1->addCell(1750)->addText('Project ID',$thName);
    $table1->addCell(1750)->addText('#',$thName);
    foreach ($id_array_explode as $char) {
        $table1->addRow();
        $singlenumber = (int)$char;
        $cursor = $collection->find(array("id" => $singlenumber));
        foreach ($cursor as $document) {
            if (is_array($document['input'])) {
                foreach ($document['input'] as $samples) {
                    $table1->addCell(1750)->addText($document["project_id"]);
                    $table1->addCell(1750)->addText($samples['nr']);
                }
            }
        }
    }
    $section->addText('        
');
    $section->addLine(
        array(
            'width'       => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16),
            'height'      => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0),
            'positioning' => 'absolute',
        )
    );
    $section->addText('Output Table', $tableName);
    $table2 = $section->addTable($spanTableStyleName);
//// THIS IS WHERE THE ERROR OCCURS!! 
        $table2->addRow(null, array('tblHeader' => true, 'cantSplit' => true));
        $table2->addCell(1750)->addText('ID',$thName);

谢谢!

解决方案 好的,所以我删除了整个文档并分别添加了每个句子,以查看错误发生的位置。这导致看到错误来自我得到的数据。它无法处理“>”和“&”符号!

因此,如果您遇到此错误,请检查您正在打印的数据!

4

3 回答 3

11

更好的解决方案是在对 word 文档进行任何操作之前添加以下代码行:

PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);

这将自动转义任何有问题的字符。

于 2018-03-23T15:33:06.190 回答
3

事实上,它来自您的数据:您在其中有一个 XML 特殊字符,当 Word 解析您的文档时,它无法理解。我通过使用解决了这个问题htmlspecialchars()。我不确定这是最好的方法,但它确实有效。

于 2016-11-22T16:56:48.983 回答
0

对 PHPWord 不是很熟悉,但请确保文档的编码和插入其中的数据相同。用于创建 excel 文件的旧库曾经有同样的问题。

于 2016-10-28T08:56:33.740 回答