PHPExcel导出mysql数据库数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHPExcel导出mysql数据库数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2370字,纯文字阅读大概需要4分钟。
内容图文
![PHPExcel导出mysql数据库数据](/upload/InfoBanner/zyjiaocheng/456/49bf5d9c020740469042e145670aef5d.jpg)
首先看看上面的文章,写入方法一样,只是中间多了数据库的操作,后面还有excel浏览器输出。
数据库代码(数据库配置文件自行完成)
<?php /*db.php*/ require dirname(__FILE__)."/dbconfig.php"; class db{ public $conn = null; public function __construct($config){ $this->conn=mysql_connect($config['host'],$config['username'],$config['password']) or die(mysql_error()); mysql_select_db($config['database'],$this->conn) or die(mysql_error()); mysql_query("set names ".$config['charset']) or die(mysql_error()); } public function getResult($sql){ $resource = mysql_query($sql,$this->conn) or die(mysql_error()); $res = array(); while(($row=mysql_fetch_assoc($resource))!=false){ $res[] = $row; } return $res; } public function getUserinfo(){ $sql = "。。。"; $res = self::getResult($sql); return $res; } }
<?php $dir = dirname(__FILE__); require $dir."/db.php"; require $dir."/PHPExcel.php"; $db = new db($phpexcel); $objPHPExcel = new PHPExcel(); for($i=0; $i<3; $i++){ if($i>0){ $objPHPExcel->createSheet(); } $objPHPExcel->setActiveSheetIndex($i); $objSheet = $objPHPExcel->getActiveSheet(); $data = $db->getUserinfo(); $objSheet->setCellValue("A1","编号")->setCellValue("B1","登陆名") ->setCellValue("C1","昵称")->setCellValue("D1","电子邮箱") ->setCellValue("E1","学校")->setCellValue("F1","最后登陆时间"); $j = 2; foreach ($data as $key => $value) { # code... $objSheet->setCellValue("A".$j,$value['id'])->setCellValue("B".$j,$value['user_login']) ->setCellValue("C".$j,$value['user_nicename'])->setCellValue("D".$j,$value['user_email']) ->setCellValue("E".$j,$value['sch_name'])->setCellValue("F".$j,$value['last_login_time']); $j++; } } $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel5"); // $objWriter->save($dir.'/export.xls'); //生成excel文件 browser_export("Excel5","browser_excel03.xls"); //浏览器输出 $objWriter->save("php://output"); function browser_export($type, $filename){ if($type == "Excel5"){ header('Content-Type: application/vnd.ms-excel'); //excel2003 }else{ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //excel2007 } header('Content-Disposition: attachment;filename="'.$filename.'"'); header('Cache-Control: max-age=0'); }
运行截图:
版权声明:本文为博主原创文章,未经博主允许不得转载。
PHPExcel导出mysql数据库数据
标签:excel php
本文系统来源:http://blog.csdn.net/qq_20480611/article/details/47987591
内容总结
以上是互联网集市为您收集整理的PHPExcel导出mysql数据库数据全部内容,希望文章能够帮你解决PHPExcel导出mysql数据库数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。