PHP method for creating Word documents (platform-independent), _ PHP Tutorial

Source: Internet
Author: User
PHP method for creating Word documents (platform-independent ),. This document describes how to create a word document in PHP (platform-independent. For your reference, refer to: how to use PHP to generate word files and find PHP to create word documents on the Internet (platform-independent ),

This example describes how to create a word document in PHP. We will share this with you for your reference. The details are as follows:

About using PHP to generate word, I found a lot of information on the Internet, including calling the COM component and installing PHP extension. This is a simple and cross-platform method.

The following code details:

Class. word. php

<? Phpclass Word {function start () {ob_start (); // enable the echo of the output control buffer'';} Function save ($ path) {echo""; $ Data = ob_get_contents (); // return the content of the output buffer ob_end_clean (); // clear the buffer and disable the output buffer $ this-> writeFile ($ path, $ data); // write the buffer content to the word} function writeFile ($ fn, $ data) {$ fp = fopen ($ fn, "wb + "); fwrite ($ fp, $ data); fclose ($ fp );}}

Index. php

<? Phpinclude ("class. word. php "); $ word = new Word (); $ word-> start (); // The following content is saved in the WORD file. Can I use the HTML tag?> Directly use php to create a word document by axgle
  

If you open data.doc and see the introduction here, the word document is created successfully.

In any operating system, you can use PHP to generate Word documents. It's definitely not a blow! A word file can be generated even if no word is installed. Of course, the generated word file can be opened with word, wps or other software.

Usage:
Use $ word-> start () to generate a word file. Then you can output any HTML code, whether it is read from the file and written here, or output HTML directly here.

After the output is complete, use the $ word-> save ($ path) method. $ path is the name of the word file you want to generate (the complete path can be provided ). when you use the $ word-> save () method, any output after this is irrelevant to the word file, that is, the word generation is complete. And then pull it in the same way as you normally use php. Anything you output is directly output in the browser instead of written into the word.

This is a very interesting method I have come up with. its implementation method is surprisingly simple, and it avoids dependency on the windows environment.
Haha, is it interesting? Enjoy it!

<? Php // the above content will be saved in the WORD file $ word-> save ("data.doc"); // save and end the word. // the following Content is normally output in the page file header ("Content-type: text/html; charset = utf-8"); echo 'data.doc is generated successfully, please go to the directory to view
';?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.