PHP Web page generates an instance program for a Word document

Source: Internet
Author: User

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. " &nbsp;&nbsp; ";
$cout. = "Problem Difficulty:". $row->difficulty. " <br> ";
$cout. = "&nbsp;&nbsp;&nbsp;". $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>

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.