Php generate word two methods _ PHP Tutorial

Source: Internet
Author: User
Php generates word in two ways. Php Tutorial to generate word two ways 1. normal touch create word2.fopen open word3.fwrite write and save this will cause a problem if the written content contains html code php Tutorial generate word two methods

1. normal touch creates word

2. open word in fopen

3. fwrite write into word and save

In this case, a problem occurs. if the written content contains html code, it will be directly written into word instead of typographical.

To solve this problem, you need to add a piece of code in the header of the output html code.

$ Headert ='Xmlns: w = "urn: schemas-microsoft-com: office: word"
Xmlns = "http://www.w3.org/TR/REC-html40"> ';
$ Footer ="";

For example, your content is $ text;

$ Text = $ header. $ text. $ footer;

In this way, everything in fck can be output in typographical style!


Method 1

$ Word = new com ("word. application") or die ("unable
Create word document ");
Print "loaded word, version {$ word-> version} n ";
$ Word-> visible = 0;
$ Word-> documents-> add ();

// An error occurred while setting the margin.
// $ Word-> selection-> agesetup-> rightmargin = '3 "';

// Set the font
$ Word-> selection-> font-> name = 'Helvetica ';

// Set the font size
$ Word-> selection-> font-> size = 8;

// Set the color
$ Word-> selection-> font-> colorindex = 13; // wddarkred = 13

// Output to document
$ Word-> selection-> typetext ("hello world ");
$ Range = $ word-> activedocument-> range (0, 0 );
$ Table_t = $ word-> activedocument-> tables-> add ($ range, 3, 4 );
$ Table_t-> cell (1, 2)-> range-> insertafter ('aaa ');
// Save
// $ Word-> sections-> add (1 );
$ Word-> documents [1]-> saveas (dirname (_ file _). "/create_test.doc ");

// Exit
$ Word-> quit ();

?>

Method 2

Class word
{
Function start ()
{
Ob_start ();
Print'Xmlns: w = "urn: schemas-microsoft-com: office: word"
Xmlns = "http://www.w3.org/TR/REC-html40"> ';
}
Function save ($ path)
{
Print"";
$ Data = ob_get_contents ();
Ob_end_clean ();
$ This-> wirtefile ($ path, $ data );
}
Function wirtefile ($ fn, $ data)
{
$ Fp = fopen ($ fn, "wb ");
Fwrite ($ fp, $ data );
Fclose ($ fp );
}
}
?>

Call method

$ Word = new word;
$ Word-> start ();
Echo $ cout;
$ Wordname = "word/". time (). ". doc ";
$ Word-> save ($ wordname); // save the word and finish


Tip 1. normal touch create word 2. fopen open word 3. fwrite write word and save this will cause a problem if the written content contains html code...

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.