Mysql数据库导出替Excel(ODBC驱动)

php中文网
发布: 2016-06-13 10:42:52
原创
1328人浏览过

Mysql数据库导出为Excel(ODBC驱动)

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php date_default_timezone_set('Asia/Shanghai');        /*     [email&#160;protected]:[email&#160;protected]     *在本程序不侵犯任何公司或个人权利的情况下:     *本程序遵循GPL v2许可证分发,详情查看http://www.gnu.org/copyleft/lesser.html     *如果本程序已经使用他人专利,本程序将按照专利持有人许可进行使用     *如果本程序侵犯了您的专利,请及时联系作者(上方邮箱即可),本人及时会做出更正声明,如有不便敬请谅解。     *本类实现:     *只要填写正确的<a style="color:#f60; text-decoration:underline;" title= "mysql"href="https://www.php.cn/zt/15713.html" target="_blank">mysql数据库、表信息自动将Mysql数据库的内容导出为Excel文件,一键完成,需要ODBC的支持,此为Windows版本     *注意:填写的时候Excel文件只需要填写文件名即可,导出Excel为是2003版本。     *          因为本人水平有限,必须按照示例程序的样本填写完毕方可顺利执行     *          本版本中存在已知瑕疵:     *          1、Mysql数据库出错会输出错误信息。Release版本请自行删除保证服务器信息安全     *          2、odbc连接部分没有做出错误处理,Release版本请自行添加错误处理部分(mysql一样)    */    class mysql2Xls{        private $currDir;        //当前目录名使用dirname(__FILE__)获取        private $srcData;    //Mysql获取的数据数组,此为2维数组        private $srcColumn;    //Mysql数据库的列名,为了将列名对应到Excel文件中的第一行,此属性为1维数组        public $xlsName;            //Excel文件的文件名,不需要添加后缀,后缀是自动添加的,如果需要设置后缀,请在setXlsName函数中更改后缀        private $host;                //本行一下五行为Mysql的连接设置        private $userName;        private $pwd;        private $dbName;        private $tbName;                public function __construct(){            $this-&gt;currDir = dirname(__FILE__);        }                /* 获取Mysql的列名称 */        public function getSrcColumn(){            $handle = @mysql_connect($this-&gt;host,$this-&gt;username,$this-&gt;pwd);            $this-&gt;srcColumn = array();            if(is_bool($handle)){                echo "====**====<br>";                echo "Mysql连接出错.<br>文件:".(__FILE__)." <br>行号:".(__LINE__)."<br>错误:".mysql_error();                echo "<br>====**====<br>";                exit(0);            }            $ret = @mysql_select_db($this-&gt;dbName,$handle);            if($handle == FALSE){                echo "====**====<br>";                echo "Mysql选择数据库出错.<br>文件:".(__FILE__)." <br>行号:".(__LINE__)."<br>错误:".mysql_error();                echo "<br>====**====<br>";                exit(0);            }            $ret = @mysql_query('describe '.$this-&gt;tbName,$handle);            if(is_bool($ret)){                echo "====**====<br>";                echo "Mysql语句执行出错.<br>文件:".(__FILE__)." <br>行号:".(__LINE__)."<br>错误:".mysql_error();                echo "<br>====**====<br>";                exit(0);            }            while(($data = mysql_fetch_array($ret)) != FALSE){                array_push($this-&gt;srcColumn,$data['Field']);            }            mysql_free_result($ret);            mysql_close($handle);            return $this-&gt;srcColumn;        }                /* 获取指定的Mysql数据库表中的内容 */        public function getSrcData(){            $handle = @mysql_connect($this-&gt;host,$this-&gt;username,$this-&gt;pwd);            $this-&gt;srcData = array();            if(is_bool($handle)){                echo "====**====<br>";                echo "Mysql连接出错.<br>文件:".(__FILE__)." <br>行号:".(__LINE__)."<br>错误:".mysql_error();                echo "<br>====**====<br>";                exit(0);            }            $ret = @mysql_select_db($this-&gt;dbName,$handle);            if($handle == FALSE){                echo "====**====<br>";                echo "Mysql选择数据库出错.<br>文件:".(__FILE__)." <br>行号:".(__LINE__)."<br>错误:".mysql_error();                echo "<br>====**====<br>";                exit(0);            }            $ret = @mysql_query('select * from '.$this-&gt;tbName,$handle);            if(is_bool($ret)){                echo "====**====<br>";                echo "Mysql语句执行出错.<br>文件:".(__FILE__)." <br>行号:".(__LINE__)."<br>错误:".mysql_error();                echo "<br>====**====<br>";                exit(0);            }            while(($data = mysql_fetch_array($ret)) != FALSE){                array_push($this-&gt;srcData,$data);            }            mysql_free_result($ret);            mysql_close($handle);            return $this-&gt;srcData;        }                /* 主力部分 将Mysql数据库中的内容导出为Excel文件 */        public function m2Xls(){            $handle = odbc_connect("Driver={MicroSoft Excel Driver (*.xls)};READONLY=false;CREATE_DB="".$this-&gt;xlsName."";Dbq=".$this-&gt;xlsName,"","");            $sql = 'create table '.$this-&gt;tbName.'(';            foreach($this-&gt;srcColumn as $value){                $sql .= ($value." text,");            }            $sql = substr($sql,0,strlen($sql)-1);            $sql .= ")";            $ret = odbc_exec($handle,$sql);            //以上内容是创建一个Excel文件 并将Mysql的列转移到Excel文件中去。            foreach($this-&gt;srcData as $value){                $sql = 'insert into ['.$this-&gt;tbName.'$] values(';                for($i=0;$i<count .="iconv(&quot;UTF-8&quot;,&quot;GB2312&quot;,&quot;'&quot;.$value[$i].&quot;',&quot;);" substr echo odbc_exec public function setmysqlargs>host = $host;            $this-&gt;username = $username;            $this-&gt;pwd = $pwd;            $this-&gt;dbName = $dbName;            $this-&gt;tbName = $tbName;        }                /* 设置Excel数据库的名称 */        public function setXlsName($xlsName){            $this-&gt;xlsName = $this-&gt;currDir."\".$xlsName.".xls";            return $this-&gt;xlsName;        }    }        /*以下部分是测试功能的,可删除*/    header("Content-Type:text/html;charset=UTF-8");    $mysql2xls = new mysql2Xls();    $mysql2xls-&gt;setMysqlArgs('localhost','root','******','others','zhuifeng');    $mysql2xls-&gt;setXlsName('test');    $mysql2xls-&gt;getSrcColumn();    $mysql2xls-&gt;getSrcData();     $mysql2xls-&gt;m2Xls();?&gt;<div class="clear"></div></count>
登录后复制
相关标签:
WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号