Php tutorials generate word in two ways
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
<? Php
$ 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
<? Php
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