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...