php腳本如何生成Word文檔,下面的程序是用來生產試卷的簡單PHP代碼
<?php
//初始化session
session_start();
// 包含數據庫連接文件和頭文件
?>
<Html>
<head>
<title>試卷生成</title>
<link href="CSS/style.css" rel="stylesheet" type="text/CSS">
</head>
<?
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(); //返回數組
$i=0; //數組下標
$query_result=@MySQL_query($sql,$conn);
while($row=@MySQL_fetch_object($query_result))
{
$i++;
$cout.=$i." ";
$cout.="題目難度:".$row->difficulty."<br>";
$cout.=" ".$row->content."<br><br>";
}//while
?>
<?PHP
class Word
{
function start()
{
ob_start();
print'<Html XMLns:o="urn:schemas-microsoft-com:office:Office"
XMLns:w="urn:schemas-microsoft-com:Office:Word"
XMLns="http://www.w3.org/TR/REC-Html40">';
}
function save($path)
{
print "</Html>";
$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);//保存Word並且結束.
?>
<div align="center"><a href="<?PHP echo $Wordname ; ?>" target=_blank class="unnamed1">試卷已經生成,請點擊這裡查看</a>
<?PHP
}
else
{
?>
</div>
<div align="center"><span class="unnamed1">您輸入的條件不足,請重新輸入!</span>
<?PHP
}
?>
</div>
</Html>