PHP操作:将数据库中的数据保存到Word、Excel中。

1.首先要把word、excel表放到文件的根目录下

2.定义了一个word类

<?php
class word
{ 

   function start()
    {
      ob_start();        ob_start — 打开输出控制缓冲
     }

    function save($path)
      {
        $data = ob_get_contents();     ob_get_contents — 返回输出缓冲区的内容
        ob_end_clean();             ob_end_clean — 清空(擦除)缓冲区并关闭输出缓冲
        $this->wirtetoword($path,$data);
     }
   function wirtetoword ($fn,$data)
     {
       $fp=fopen($fn,"w");  fopen — 打开文件或者 URL,第二个参数是表示以什么方式打开
       fwrite($fp,$data);        fwrite — 写入文件(可安全用于二进制文件)
       fclose($fp);      fclose — 关闭一个已打开的文件指针
    }
}
?>

2.引用该类,并从数据库中调用nation表的数据

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
</head>
<body>
    <?php
        if(!isset($_GET["id"]))
        {
            ?>
            <input type="button" name="submit" value="将表格内容保存到Word" onclick="window.location.href='main.php?id=print'">
        <?php
}
        ?>
    <?php
    if($_GET["id"]!="")
    {
        include("word.class.php");  将word类引用进来
            $word=new word();   对该类进行实例化
            $word->start();      调用start()方法,定义要保存表格的开始
        
        
    }
        
        
        ?>
        
        <table border="1" cellspacing="0" cellpadding="0">
            <tr><th>代号</th><th>名称</th></tr>
            <?php
                include("../gongju/DBDA.class.php");
                $db=new DBDA();
                $sql="select * from nation";
                $attr=$db->Query($sql);
                foreach($attr as $v)
                {
                    echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td></tr>";
                }
                
                ?>
        </table>
        <?php
        if($_GET["id"]!=""){
            $word->save("data.doc");    保存表格结束
        }
            
?>
            
        
</body>
</html>

 

转载于:https://www.cnblogs.com/xingyue1988/p/6388855.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值