保存Excel

excel在办公系统中得到广泛的应用,通过程序实现从数据库中将数据导入到Excel表格内是一中行之有效的方法;

本实例的关键是Excel类的定义和应用,该类关键代码如下

<?php
class excel{
    function start(){
        ob_start();
    }
    function save($path){
        $data = ob_get_contents();
        ob_end_clean();
        $this->wirtetoexcel($path,$data);
    }
    function wirtetoexcel ($fn,$data){
        $fp=fopen($fn,"wb");
        fwrite($fp,$data);
        fclose($fp);
   }
}?>

该类中定义了三个成员函数,其中

start()用于限定要保存的数据的开始;

save()用于限定要保存的数据的结束,

并调用成员函数writetoexcel()实现将查询结果一二进制的形式保存到Excel表格中;

下面是数据库连接和页面显示代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <?php

 //实现数据库连接
    $conn=@mysql_connect("localhost","root","") or die("连接失败");
    mysql_select_db("test",$conn) or die("连接失败");
    mysql_query("set names utf8");
    $sql=@mysql_query("select * from tb_score where sno like '%".$sno."%'",$conn);
    $info=mysql_fetch_array($sql);
    // if($_POST[submit]!="" || $_GET[id]!=""){//这个判断没有数据显示,故不使用
    if($info){
        $sno=@$_POST["sno"];
        if(@$_GET["sno"]!=""){
            $sno=$_GET["sno"];
        }
    var_dump($info);
    if($info==false){
        echo "<div align=center>对不起,没有查找到您要找的学生成绩信息!</div>";
    }else{
?>
    <table width="500" height="25" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
    <td bgcolor="#BADA0D">
    <?php
        if(@$_GET["id"]!=""){
            $Excel=new Excel();
            $Excel->start();
        }
   ?>
    <table width="500" height="50" border="0" align="center" cellpadding="0" cellspacing="1">
    <tr>
        <td width="96" height="25" bgcolor="#E6F2A8"><div align="center">学号</div></td>
        <td width="75" bgcolor="#E6F2A8"><div align="center">姓名</div></td>
        <td width="81" bgcolor="#E6F2A8"><div align="center">班级</div></td>
        <td width="78" bgcolor="#E6F2A8"><div align="center">语文</div></td>
        <td width="88" bgcolor="#E6F2A8"><div align="center">数学</div></td>
        <td width="75" bgcolor="#E6F2A8"><div align="center">外语</div></td>
    </tr>
    <?php
         do{
   ?>
    <tr>
       <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info["sno"];?></div></td>
       <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info["sname"];?></div></td>
       <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info["sclass"];?></div></td>
       <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info["yw"];?></div></td>
       <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info["sx"];?></div></td>
       <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info["wy"];?></div></td>
    </tr>
    <?php }
        while($info=mysql_fetch_array($sql));
     ?>
    <?php
        if(@$_GET["id"]!=""){
            $Excel->save("data.xls");
        }
    ?>
    <tr>
        <td height="25" colspan="6" bgcolor="#FFFFFF"><div align="center">
    <?php
        if(@$_GET["id"]==""){
    ?>

    <input type="button" value="将表格内容保存到Excel" class="buttoncss" onclick="window.location.href='yzm.php?id=print&sno=<?php echo $sno?>'">
    <?php }
        else{
            echo "<div align=center>查询结果已经保存到Excel中</div>";
       }
    ?>
    </div></td>
    </tr>
    </table>
 <?php
       }
  }
 ?>
</body>
</html>

 

posted @ 2018-05-09 11:43  TangYJun  阅读(206)  评论(0)    收藏  举报