In this article, we mainly address two questions:
1: How to build HTML content into a Word document in PHP
2:php when the content in HTML is generated in a Word document, the problem is not centered, which is displayed by default in Web view.
3:php when you build content in HTML into a Word document, the related styles are incompatible.
Body:
The code is as follows |
Copy Code |
Echo ' Echo ' <body><table class= "Table_dayin" > <caption class= "Table_caption" >; echo "Digital teaching system electronic preparation manuscript <br> <span> discipline <em style= "border-bottom:1px solid #545454;" > Language </em> School <em style= "border-bottom:1px solid #545454;" > Experimental Secondary School </em></span> </caption> "; Echo ' </table></body> Ob_start (); Open buffer Header ("Cache-control:public"); Header ("Content-type:application/octet-stream"); Header ("Accept-ranges:bytes"); if (Strpos ($_server["http_user_agent"], ' msie ') { Header (' content-disposition:attachment; Filename=test.doc '); }else if (Strpos ($_server["http_user_agent"], ' Firefox ')) { Header (' content-disposition:attachment; Filename=test.doc '); } else { Header (' content-disposition:attachment; Filename=test.doc '); } Header ("Pragma:no-cache"); Header ("expires:0"); Ob_end_flush ()//output all content to browser |
Note: The above Code section provides the ability to generate content in a PHP program file into a Word document and provide downloads.
Example
The code is as follows |
Copy Code |
<?php //initialization session Session_Start (); //Contains database connection files and header files ?> <title> quiz generation </title> <link HR ef= "Css/style.css" rel= "stylesheet" type= "Text/css" > include (' head.php '); require (' dbconnect.php '); ?> . PHP $juge = 0; for ($i =1; ($i <100); $i + +) { $a = $i; if (isset ($_post[$a)) { if ($juge ==0) $sql. = "id=". $_post[$a]; Else $sql. = "or id=". $_post[$a]; $juge = 1; } } if ($sql!= "") { $sql = "SELECT * FROM Test WHERE". $sql; $result _array=array ();//return array $i = 0;//array subscript $query _result= @mysql_query ($sql, $conn); while ($row = @mysql_fetch_object ($query _result)) { $i + +; $cout. = $i. " "; $cout. = "Problem Difficulty:". $row->difficulty. " <br> "; $cout. = " ". $row->content. " <br><br> "; }//while ?> <?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); } } /*-------Word class end-------*/ $word =new Word; $word->start (); Echo $cout; $wordname = "word/". Time (). ". Doc "; $word->save ($wordname);//save Word and end. ?> <div align= "center" ><a href= "<?php echo $wordname;?>" Target=_blank class= "unnamed1" > The quiz paper has been generated, please click here to view </a> . PHP } Else { ?> </div> <div align= "center" ><span class= "unnamed1" > The bar you entered Not enough, please re-enter! </span> . PHP } ?> </div> |