这是我的代码:
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$section->addText('Goodby WOrld!');
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=byeWorld.docx");
header("Content-Type: application/docx");
header("Content-Transfer-Encoding: binary");
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('byeWorld.docx');
正在下载文件(byeWorld.docx),但该文件为空。
这里有什么问题?
答案 0 :(得分:1)
您要保存到服务器上的文件('byeWorld.docx'
),而不是发送到浏览器,因此无需设置标题。
如果您要发送到浏览器,那么您应该保存到'php://output'
, 然后 您需要标题
答案 1 :(得分:0)
您的标头看起来有问题。根据我读过的例子,你不需要它们。或者,另一种可能性,不要在创建文档的过程中声明它们。试试顶部?并确保您require
PHPWord.php
位于顶部。
来自http://phpword.codeplex.com/documentation:
// Include the PHPWord.php, all other classes were loaded by an autoloader
require_once 'PHPWord.php';
// Create a new PHPWord Object
$PHPWord = new PHPWord();
// Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection();
// After creating a section, you can append elements:
$section->addText('Hello world!');
// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
// If you often need the same style again you can create a user defined style to the word document
// and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle');
// You can also putthe appended element to local object an call functions like this:
$myTextElement = $section->addText('Hello World!');
$myTextElement->setBold();
$myTextElement->setName('Verdana');
$myTextElement->setSize(22);
// At least write the document to webspace:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');