1

我已经按照这里关于在 Codeigniter 中安装 PHPWord 的所有说明进行操作

我只是不知道在哪里可以找到我的输出文件(filename.docx)以及如何知道它是否正常工作。没有错误信息。

这是我的代码:

  $PHPWord = $this->word;
  $section = $PHPWord->createSection(array('orientation'=>'landscape'));
  $section->addText('Hello World!');
  $section->addTextBreak(2);

  $section->addText('I am inline styled.', array('name'=>'Verdana', >'color'=>'006699'));
  $section->addTextBreak(2);

  $PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, >'size'=>16));
  $PHPWord->addParagraphStyle('pStyle', array('align'=>'center', >'spaceAfter'=>100));
  $section->addText('I am styled by two style definitions.', 'rStyle', >'pStyle');
  $section->addText('I have only a paragraph style definition.', null, >'pStyle');

  $filename='kem.docx'; //save our document as this file name
  header('Content-Type: application/vnd.openxmlformats->officedocument.wordprocessingml.document'); //mime type
  header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell >browser what's the file name
  header('Cache-Control: max-age=0'); //no cache

  $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
  $objWriter->save('php://output');

顺便说一句,我正在使用 ubuntu 12.04 LTS

4

1 回答 1

1

代码中有一些错误。在所有样式数组中都有额外的 >s。也许只是复制和粘贴错误...

因为您输出到 php://output,所以不会写入任何文件,它只会直接发送到浏览器。而是使用类似 $objWriter->save('d:\www\' . $filename); 创建输出文件。

使用以下代码一切正常。它将输出到文件并将其发送到浏览器。

<?php
require_once 'PHPWord.php';

$PHPWord = new PHPWord();
$section = $PHPWord->createSection(array('orientation'=>'landscape'));
$section->addText('Hello World!');
$section->addTextBreak(2);


$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);

$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
$section->addText('I have only a paragraph style definition.', null, 'pStyle');

$filename='kem.docx'; //save our document as this file name
header('Content-Type: application/vnd.ms-word'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell >browser what's the file name
header('Cache-Control: max-age=0'); //no cache

$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
$objWriter->save('d:\www\\php\\genDoc\\' . $filename);

/* For Unix / Linux

$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
$objWriter->save('/home/ubuntu/workspace/uploads/'. $filename);

*/
?>
于 2013-11-19T11:40:34.110 回答