php生成word文檔的代碼,這個是用來生產(chǎn)試卷的簡單php代碼
<?php
//初始化session
session_start();
// 包含數(shù)據(jù)庫連接文件和頭文件
?>
<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(); //返回數(shù)組
$i=0; //數(shù)組下標
$[email protected]_query($sql,$conn);
while($[email protected]_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=";
}
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并且結(jié)束.
?>
<div align="center"><a href="<?php echo $wordname ; ?>" target=_blank class="unnamed1">試卷已經(jīng)生成,請點擊這里查看</a>
<?php
}
else
{
?>
</div>
<div align="center"><span class="unnamed1">您輸入的條件不足,請重新輸入!</span>
<?php
}
?>
</div>
</html>
|
新聞熱點
疑難解答