Home > Backend Development > PHP Tutorial > How to create word documents in PHP (platform independent), _PHP tutorial

How to create word documents in PHP (platform independent), _PHP tutorial

WBOY
Release: 2016-07-12 08:55:17
Original
776 people have browsed it

How to create a word document with PHP (platform independent),

This article describes the method of creating a word document with PHP. Share it with everyone for your reference, the details are as follows:

Regarding using PHP to generate word, I found a lot of information on the Internet. Some are generated by calling COM components, and some are generated by installing PHP extensions. No need to worry, the following is a relatively simple method, and can be cross-platform.

The following is the detailed code:

class.word.php

<&#63;php
class Word{
  function start(){
    ob_start(); //打开输出控制缓冲
    echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"';
    echo 'xmlns:w="urn:schemas-microsoft-com:office:word"';
    echo 'xmlns="http://www.w3.org/TR/REC-html40">';
  }
  function save($path){
    echo "</html>";
    $data=ob_get_contents(); //返回输出缓冲区的内容
    ob_end_clean(); //清空缓冲区并关闭输出缓冲
    $this->writeFile($path,$data); //将缓冲区内容写入word
  }
  function writeFile($fn,$data){
    $fp=fopen($fn,"wb+");
    fwrite($fp,$data);
    fclose($fp);
  }
}

Copy after login

index.php

<&#63;php
include("class.word.php");
$word=new Word();
$word->start();
//以下内容会保存在WORD文件中,可以使用HTML标签
&#63;>
 <h1>直接用php创建word文档</h1>
 作者:axgle
<hr size=1>
 <p>如果你打开data.doc,看到了这里的介绍,则说明word文档创建成功了。
<p>
不论是在什么操作系统下,使用本方法都可以直接用PHP生成word文档。绝对不是吹牛!
就算是没有安装word,也能够生成word文件。
当然了,生成的word文件可以用word,wps或者其他软件打开。
<p>
<b>使用方法:</b>
<br>
首先用$word->start()表示要生成word文件了。
然后你可以输出任何的HTML代码,不论是从文件读过来再写到这里,
还是直接在这里输出HTML,都没有关系。
<p>等你输出完毕后,用$word->save($path)方法,其中$path是你想
生成的word文件的名称(可以给出完整的路径).当你使用了$word->save()
方法后,这后面的任何输出都和word文件没有关系了,也就是说word的生成
工作就完成了。之后就和你平常使用php的方式一样拉。随便你输出什么东西,
都直接在浏览器里输出,而不会写到word里面去。
<p>这是本人想到的一个很有意思的方法,它的实现方法出人意料的简单,并且避免
了对windows环境的依赖。
<br>哈哈,很有意思吧?享受它吧!
<hr size=1>
<&#63;php
//以上内容会保存在WORD文件中
$word->save("data.doc");//保存word并且结束.
//以下内容正常输出在页面文件中
header("Content-type:text/html;charset=utf-8");
echo 'data.doc生成成功,请到目录下查看<br>';
&#63;>

Copy after login

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP Network Programming Skills", "Introduction Tutorial on PHP Basic Grammar", "Summary of PHP Office Document Skills (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary" 》

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • PHP realizes Baidu library imitation, Docin online document effect (word, excel, ppt to flash)
  • Used under Windows system Tutorial on generating Word documents with PHP
  • php reads word documents and displays them through baihui.com API
  • Simple sample code for php to export word documents and excel spreadsheets
  • Use PHP Principles and examples of exporting Word documents
  • php generates word documents from web pages in the program and provides downloadable code
  • The code for exporting web pages into Word documents in PHP
  • is in Code for reading and writing WORD documents in PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117081.htmlTechArticleHow to create word documents with PHP (platform independent). This article describes the method of creating word documents with PHP. Share it with everyone for your reference, the details are as follows: Regarding using PHP to generate word, find it online...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template