PHPWord_Template保存到客户端

时间:2013-01-28 05:03:00

标签: codeigniter phpword

我从this site下载了PHPWord_0.6.2_Beta.zip包。
有2个目录:示例和PHPWord,以及1个文件:PHPWord.php
我把应用程序/ third_party中的PHPWord目录和PHPWord.php并在应用程序/库中创建名为“Word.php”的新库,如this article中所述

这是新的库代码Word.php < BR />

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH."/third_party/PHPWord.php"; 

class Word extends PHPWord { 
    public function __construct() { 
        parent::__construct(); 
    } 
}
?>


现在我们可以轻松地使用PHPWord作为CI库了
我已经从下载包中的目录示例中尝试了Text Example,这里是原始代码

<?php
require_once '../PHPWord.php';

// New Word Document
$PHPWord = new PHPWord();

// New portrait section
$section = $PHPWord->createSection();

// Add text elements
$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');



// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Text.docx');
?>


我尝试在我的CI控制器中实现它,这里是代码PHPWord_Text.php

<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');
class PHPWord_Text extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('word');
    }
    function index() {
        $PHPWord = $this->word; // New Word Document
        $section = $PHPWord->createSection(); // New portrait section
        // Add text elements
        $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');
        // Save File / Download (Download dialog, prompt user to save or simply open it)
        $filename='just_some_random_name.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');
    }
}
?>

中访问它
http://localhost/codeigniter/index.php/PHPWord_Text


Foilaa !!!我的代码有效!但是......当我试图将目录示例中的模板示例从下载的包转换为新的CI控制器时,我有点困惑,这里是Template.php的原始代码

<?php
require_once '../PHPWord.php';

$PHPWord = new PHPWord();

$document = $PHPWord->loadTemplate('Template.docx');

$document->setValue('Value1', 'Sun');
$document->setValue('Value2', 'Mercury');
$document->setValue('Value3', 'Venus');
$document->setValue('Value4', 'Earth');
$document->setValue('Value5', 'Mars');
$document->setValue('Value6', 'Jupiter');
$document->setValue('Value7', 'Saturn');
$document->setValue('Value8', 'Uranus');
$document->setValue('Value9', 'Neptun');
$document->setValue('Value10', 'Pluto');

$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));

$document->save('Solarsystem.docx');
?>


任何人都可以帮我解决这个问题吗?请.. T_T
注意:我不希望它保存在服务器中,我希望它显示下载对话框提示用户更好地保存或打开它,就像这里的一些代码一样

// Save File / Download (Download dialog, prompt user to save or simply open it)
$filename='just_some_random_name.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');

5 个答案:

答案 0 :(得分:2)

我认为您在使用Template.php时可能会遇到多个问题。

首先,你可能想弄清楚它在做什么。

使用其他示例x.php生成x.docx。对于Template.php,它使用Template.docx生成Solarsystem.docx。

变量Value1,Value2,Value3等都在Template.docx中定义为$ {Value1},$ {Value2},$ {Value3}等。如果更改Template.php右侧的值,它将更改Solarsystem.docx中的相应值。

其次,你似乎关注输出。

对我有用的是:

$filename = 'nameOfFile.docx';
$document->save($filename);
header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);

这种方式仍然可以将其保存在服务器上,但也可以下载它。如果我同时使用$document->save();$objWriter->save();,当我尝试在objWriter保存的文件上使用readfile();时,我会收到一个空白文档。

答案 1 :(得分:1)

我认为这是正确的:

 $filename = 'nameOfFile.docx';
    $document->save($filename);
    header('Content-Description: File Transfer');
    header('Content-type: application/force-download');
    header('Content-Disposition: attachment; filename='.basename($filename));
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($filename));
    readfile($filename);
    unlink($filename);

答案 2 :(得分:0)

save()类的方法Template返回临时文件文档的名称,因此您只需将其提供给readfile($tempName)的用户;

答案 3 :(得分:-1)

我不确定我的问题是否正确,如果我错了,请原谅。

在你的第四个代码块中你有一行

$document->save('Solarsystem.docx');

如果您不想将文件保存到本地磁盘,而是将其发送到客户端,则只需删除该行并添加第5个代码块中的代码:

// Save File / Download (Download dialog, prompt user to save or simply open it)
$filename='just_some_random_name.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');

答案 4 :(得分:-1)

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once APPPATH . '/src/PhpWord/Autoloader.php';

use PhpOffice\PhpWord\Autoloader as Autoloader;
Autoloader::register();