打开文件时 PHPOffice Word 生成器问题

PHPOffice Word generator problem when opening file

每当我尝试下载由 PHPWord 生成的文件时,都会弹出此错误

您可以从这里下载源代码 - "https://github.com/PHPOffice/PHPWord" or also prefar this one 'https://buildmedia.readthedocs.org/media/pdf/phpword/latest/phpword.pdf'

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpword, 'Word2007');
$filename = 'helloWorld'.date("h:i:s").'.docx';
$objWriter->save($filename);

可能是你的 ms word 有问题。尝试在线查看或其他方式。

下面也试试

1.Add setCompatibility() 在 createWriter()

之前
\PhpOffice\PhpWord\Settings::setCompatibility(false);

2。在 save()

之后添加 exit;
// XML Writer compatibility
\PhpOffice\PhpWord\Settings::setCompatibility(false);

    // Saving the document as OOXML file...
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    ob_clean();
    $objWriter->save('php://output');

    exit;

只需添加ob_clean();在输出之前!