Heim  >  Artikel  >  php教程  >  使用php创建word文档的例子

使用php创建word文档的例子

WBOY
WBOYOriginal
2016-05-25 16:40:311197Durchsuche

下面来看一个使用php创建word文档的例子的,创建word文件处理类非常的简单,我们只要加载起来然后创建类再生成就可以了,例子代码如下:

<?php
include ("word.php");
$word = new word;
$word->start();
?>

直接用php创建word文档,如果你打开word.doc,看到了这里的介绍,则说明word文档创建成功了,不论是在什么操作系统下,使用本方法都可以直接用PHP生成word文档,绝对不是吹牛!

就算是没有安装word,也能够生成word文件,当然了,生成的word文件可以用word,wps或者其他软件打开.

使用方法:首先用$word->start()表示要生成word文件了,然后你可以输出任何的HTML代码,不论是从文件读过来再写到这里,还是直接在这里输出HTML,都没有关系.

等你输出完毕后,用$word->save($path)方法,其中$path是你想生成的word文件的名称,可以给出完整的路径,当你使用了$word->save() 方法后,这后面的任何输出都和word文件没有关系了,也就是说word的生成工作就完成了,之后就和你平常使用php的方式一样拉,随便你输出什么东西,都直接在浏览器里输出,而不会写到word里面去.

这是本人想到的一个很有意思的方法,它的实现方法出人意料的简单,并且避免了对windows环境的依赖,哈哈,很有意思吧?享受它吧.

使用php创建word文档的例子代码如下:

<?php
$word->save("data.doc"); //保存word并且结束.
echo &#39;  
<title>直接用php创建word文档</title>  
<h1>直接用php创建word文档</h1>  
生成word了吗?在你的目录下看看有没有data.doc!  
<br>  
如果你用的是windows,并且安装得有word,可以查看<p>  
<a href="data.doc" target=_blank>这里</a>&#39;;
?>

word.php文件,代码如下:

<?php
class word {
    /*
    @GNU:GPL
    @author axgle <axgle@yahoo.com.cn>
    @date 2005.4.20
    */
    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);
    } //开源代码phprm.com
    function wirtefile($fn, $data) {
        $fp = fopen($fn, "wb");
        fwrite($fp, $data);
        fclose($fp);
    }
}
?>


永久链接:

转载随意!带上文章地址吧。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn