Home >php教程 >php手册 >php生成word两种方法

php生成word两种方法

WBOY
WBOYOriginal
2016-05-25 16:38:491711browse

1.正常的touch创建word 2.fopen 打开word 3.fwrite 写入word 并保存

这样会出现一个问题,如果写入的东西里面含有html代码的话,它将直接写入word而不是 排版了.

这个问题,需要在输出html代码头部加一段代码:

<?php
$headert=&#39;<html xmlns:o="urn:schemas-microsoft-com:office:office" 
    xmlns:w="urn:schemas-microsoft-com:office:word" 
    xmlns="http://www.w3.org/tr/rec-html40">&#39;; 
    $footer="</html>";
?>

比如你的内容是$text;

那么写入的时候$text=$header.$text.$footer;

这样的话fck里面的东西就能按排版的样式输出了!

方法一,实例代码如下:

<?php 
    $word= new com("word.application") or die("unable to 
    create word document"); 
    print "loaded word, version{$word->version}n"; 
    $word->visible =0; 
    $word->documents->add(); 
    //设置边距 这个有错误 
    // $word->selection->agesetup->rightmargin =&#39;3"&#39;; 
    //设置字体 这 
    $word->selection->font->name =&#39;helvetica&#39;; 
    //设置字号 
    $word->selection->font->size = 8; 
    //设置颜色 
    $word->selection->font->colorindex= 13; //wddarkred= 13 
    //输出到文档 
    $word->selection->typetext("hello world "); 
    //开源代码phprm.com 
    $range = $word->activedocument->range(0,0); 
    $table_t =$word->activedocument->tables->add($range,3,4); 
    $table_t->cell(1,2)->range->insertafter(&#39;aaa&#39;); 
    //保存 
    //$word->sections->add(1); 
    $word->documents[1]->saveas(dirname(__file__)."/create_test.doc"); 
    //退出 
    $word->quit(); 
?>

方法二,实例代码如下:

<?php
class word {
    function start() {
        ob_start();
        print &#39;<html xmlns:o="urn:schemas-microsoft-com:office:office" 
    xmlns:w="urn:schemas-microsoft-com:office:word" 
    xmlns="http://www.w3.org/tr/rec-html40">&#39;;
    }
    function save($path) {
        print "</html>";
        $data = ob_get_contents();
        ob_end_clean();
        $this->wirtefile($path, $data);
    }
    function wirtefile($fn, $data) {
        $fp = fopen($fn, "wb");
        fwrite($fp, $data);
        fclose($fp);
    }
}
?>

调用方法,代码如下:

$word=new word; 
$word->start(); 
echo $cout; 
$wordname="word/".time().".doc"; 
$word->save($wordname);//保存word并且结束


本文地址:

转载随意,但请附上文章地址:-)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn