1. 程式人生 > php教程 > PHP生成word文件格式試卷的程式碼_PHP教程

PHP生成word文件格式試卷的程式碼_PHP教程

PHP生成word文件格式試卷的程式碼


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; //陣列下標
[email protected]_query($sql,$conn);
while([email protected]_fetch_object($query_result))
{
$i++;
$cout.=$i."  ";
$cout.="題目難度:".$row->difficulty."
";
$cout.="   ".$row->content."

";
}//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並且結束.
?>
<p align="center"><a href="<?php echo $wordname ; ?>" target=_blank class="unnamed1">試卷已經生成,請點選這裡檢視</a>
<?PHP
}
else
{
?>

<p align="center"><span class="unnamed1">您輸入的條件不足,請重新輸入!
<?PHP
}
?>

</html>