mysql数据库导出为excel(odbc驱动)
php codecurrdir = dirname(__file__); } /* 获取mysql的列名称 */ public function getsrccolumn(){ $handle = @mysql_connect($this->host,$this->username,$this->pwd); $this->srccolumn = array(); if(is_bool($handle)){ echo ====**====
; echo mysql连接出错.
文件:.(__file__).
行号:.(__line__).
错误:.mysql_error(); echo
====**====
; exit(0); } $ret = @mysql_select_db($this->dbname,$handle); if($handle == false){ echo ====**====
; echo mysql选择数据库出错.
文件:.(__file__).
行号:.(__line__).
错误:.mysql_error(); echo
====**====
; exit(0); } $ret = @mysql_query('describe '.$this->tbname,$handle); if(is_bool($ret)){ echo ====**====
; echo mysql语句执行出错.
文件:.(__file__).
行号:.(__line__).
错误:.mysql_error(); echo
====**====
; exit(0); } while(($data = mysql_fetch_array($ret)) != false){ array_push($this->srccolumn,$data['field']); } mysql_free_result($ret); mysql_close($handle); return $this->srccolumn; } /* 获取指定的mysql数据库表中的内容 */ public function getsrcdata(){ $handle = @mysql_connect($this->host,$this->username,$this->pwd); $this->srcdata = array(); if(is_bool($handle)){ echo ====**====
; echo mysql连接出错.
文件:.(__file__).
行号:.(__line__).
错误:.mysql_error(); echo
====**====
; exit(0); } $ret = @mysql_select_db($this->dbname,$handle); if($handle == false){ echo ====**====
; echo mysql选择数据库出错.
文件:.(__file__).
行号:.(__line__).
错误:.mysql_error(); echo
====**====
; exit(0); } $ret = @mysql_query('select * from '.$this->tbname,$handle); if(is_bool($ret)){ echo ====**====
; echo mysql语句执行出错.
文件:.(__file__).
行号:.(__line__).
错误:.mysql_error(); echo
====**====
; exit(0); } while(($data = mysql_fetch_array($ret)) != false){ array_push($this->srcdata,$data); } mysql_free_result($ret); mysql_close($handle); return $this->srcdata; } /* 主力部分 将mysql数据库中的内容导出为excel文件 */ public function m2xls(){ $handle = odbc_connect(driver={microsoft excel driver (*.xls)};readonly=false;create_db=\.$this->xlsname.\;dbq=.$this->xlsname,,); $sql = 'create table '.$this->tbname.'('; foreach($this->srccolumn as $value){ $sql .= ($value. text,); } $sql = substr($sql,0,strlen($sql)-1); $sql .= ); $ret = odbc_exec($handle,$sql); //以上内容是创建一个excel文件 并将mysql的列转移到excel文件中去。 foreach($this->srcdata as $value){ $sql = 'insert into ['.$this->tbname.'$] values('; for($i=0;$ihost = $host; $this->username = $username; $this->pwd = $pwd; $this->dbname = $dbname; $this->tbname = $tbname; } /* 设置excel数据库的名称 */ public function setxlsname($xlsname){ $this->xlsname = $this->currdir.\\.$xlsname..xls; return $this->xlsname; } } /*以下部分是测试功能的,可删除*/ header(content-type:text/html;charset=utf-8); $mysql2xls = new mysql2xls(); $mysql2xls->setmysqlargs('localhost','root','******','others','zhuifeng'); $mysql2xls->setxlsname('test'); $mysql2xls->getsrccolumn(); $mysql2xls->getsrcdata(); $mysql2xls->m2xls();?>