3

我已经从这个站点

下载了包 PHPWord_0.6.2_Beta.zip有 2 个目录:Examples 和 PHPWord,以及 1 个文件:PHPWord.php
我将 PHPWord 目录和 PHPWord.php 放在 application/third_party 中,并创建名为“Word. php' 在应用程序/库中,如本文所述

这是新的库代码 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 库

我已经从下载包中的目录示例中尝试了文本示例,这里是原始代码

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


福莱阿!!!我的代码有效!但是...当我尝试将下载包中的目录示例中的模板示例转换为新的 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');
4

5 回答 5

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();当我尝试对readfile();objWriter 保存的文件使用时,我得到一个空白文档。

于 2013-05-02T07:41:34.623 回答
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);
于 2013-11-26T10:28:34.447 回答
0

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

于 2014-12-09T15:23:46.230 回答
-1

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

在您的第 4 个代码块中,您有一行

$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');
于 2013-04-04T20:27:21.187 回答
-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();
于 2015-07-28T22:38:02.740 回答