如何在codeigniter中显示microsoft word内容?

时间:2015-06-29 01:26:28

标签: codeigniter

我正在使用第三方应用程序PHPWord_0.6.2_Beta在带有codeigniter的网页中显示微软的单词内容,但是我找不到教程那样做,只是找到这个教程用来创建ms word文件并在其中写入

$this->load->library('word');
 //our docx will have 'lanscape' paper orientation
$section = $this->word->createSection(array('orientation'=>'landscape'));
$section->addText('Hello I am tester');
$section->addTextBreak(1);
$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(1);
$this->word->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$this->word->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='test.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($this->word, 'Word2007');
$objWriter->save('php://output');

如何在codeigniter中将Microsoft Word内容显示到网页中?

1 个答案:

答案 0 :(得分:2)

  1. 下载PHPWord
  2. 提取Zip。(您将获得3个文件)
  3. phpword文件夹和phpword.php文件复制到third_party文件夹中的application文件夹。
  4. 在CodeIgniter的application/libraries中创建一个新库。我们称之为Word.php
  5. 将此代码粘贴到word.php
  6. <?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(); 
        } 
    }
    

    并在控制器中使用此

    <?php
    
    $this->load->library('word');//load librerry as usual
    
    
    $section = $this->word->createSection(array('orientation'=>'portrait'));
    
    $section->addText('Hello I am tester');
    $section->addTextBreak(1);
    
    $section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
    $section->addTextBreak(1);
    
    $this->word->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
    $this->word->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='test.doc'; //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($this->word, 'Word2003');
    $objWriter->save('php://output');
    

    如果这不起作用也加上这句话

    $this->output->set_header("HTTP/1.0 200 OK");
    $this->output->set_header("HTTP/1.1 200 OK");
    $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
    $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
    $this->output->set_header("Cache-Control: post-check=0, pre-check=0");
    $this->output->set_header("Pragma: no-cache");
    

    set_header Reference