zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    muduo库源码剖析(一) reactor模式
    POSIX 线程编程(二)线程建立与终止
    visual assist常用快捷键
    Linux下 静态链接库 和 动态链接库
    linux(Ubuntu)下mysql字符集完美解决
    共享内存解读
    hdu2829
    hdu3525
    2013ACM-ICPC亚洲区南京站现场赛G题
    poj1487
  • 原文地址:https://www.cnblogs.com/xingyue1988/p/6388855.html
Copyright © 2011-2022 走看看