我在我的Laravel应用程序中使用PHPOffice/PHPWord。它用于生成具有表中结果的.docx文档。这对于包含6行的3个表的文档非常有用,但是当有更多的行时,就会生成文档,但是当打开它时,会发生以下错误:
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/word的错误?
我已经做了一些故障排除,删除所有,并慢慢添加新的行。我已经发现了这个错误,但我怎样才能纠正它呢?前两个表生成得很好。
$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);谢谢!
解决方案 Oke,因此我删除了整个文档,并分别添加了每个句子,以查看错误发生在何处。这导致我发现错误来自于我所得到的数据。它不能处理">“和"&”标志!
因此,如果每个人都有这个错误,检查您正在打印的数据!
发布于 2018-03-23 15:33:06
更好的解决方案是在使用word文档之前添加以下代码行:
PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);这将自动转义任何有问题的字符。
发布于 2016-11-22 16:56:48
实际上,它来自您的数据:其中包含一个XML特殊字符,当Word解析您的文档时,它就不明白了。我用htmlspecialchars()解决了这个问题。我不确定这是最好的方法,但有效。
发布于 2016-10-28 08:56:33
不太熟悉PHPWord,但要确保文档的编码和插入的数据是相同的。用于创建excel文件的旧库也有相同的问题。
https://stackoverflow.com/questions/40301312
复制相似问题