PHPWord_Template保存到客户端 [英] PHPWord_Template save to client

查看:86
本文介绍了PHPWord_Template保存到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从此网站下载了软件包WordWord_0.6.2_Beta.zip

有2个目录:示例和PHPWord,以及1个文件:PHPWord.php
我将PHPWord目录和PHPWord.php放在application / third_party中,并在application / libraries中创建名为'Word.php'的新库本文

这是新的库代码Word.php

I have download the package PHPWord_0.6.2_Beta.zip from this site

There are 2 directory : Examples and PHPWord, and 1 file : PHPWord.php
I put PHPWord directory and PHPWord.php in application/third_party and create new library named 'Word.php' in application/libraries as explained in this article

Here is the new library code Word.php

<?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库了

我已经尝试过目录中的文本示例下载的软件包中的示例,这是原始代码


Now we can easily use PHPWord as CI library

I have tried Text Example from directory Example in downloaded package, here is the original code

<?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


And I tried implement it in my CI controller, here is the code 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的原始代码


Foilaa!!! my code works! But... I'm a bit confused when i tried to translate Template Example in directory Examples from downloaded package into a new CI controller, here is the original code of 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
注意:我不想将其保存在服务器中,我希望它显示下载对话框提示用户是否保存或打开它,就像此处的某些代码一样


Can anyone please help me with this problem? please.. T_T
NOTE : i dont want it to be saved in server, i want it to show download dialog prompt user wether to save it or open it, works like some code down here

// 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');


推荐答案

我认为您可能遇到一个以上的问题Template.php。

I think you may be experiencing more than one issue with Template.php.

首先,您可能试图弄清楚它在做什么。

First, you may be trying to figure out what it is doing.

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

With the other examples x.php produces x.docx. In the case of Template.php, it uses Template.docx to produce Solarsystem.docx.

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

The variables Value1, Value2, Value3, etc. are all defined within Template.docx as ${Value1}, ${Value2}, ${Value3}, etc. If you change the values on the right in Template.php, it will change the corresponding values in Solarsystem.docx.

第二,您似乎担心输出。

Second, you seem to be concerned with output.

对我有用的是:

$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(); 时得到一个空白文档。

This way still saves it on the server but downloads it too. If I use both $document->save(); and $objWriter->save(); I get a blank document when I try to use readfile(); on the file saved by objWriter.

这篇关于PHPWord_Template保存到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆