Export files using Phpexcel
First look at the above article, write method, just the middle of the database operation, followed by the Excel browser output.
Database code (Database configuration file self-completed)
<?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;}}
Database Export Code:
<?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", "number")->setcellvalue ("B1", "Login Name")->setcellvalue ("C1", "nickname") Setcellvalue ("D1", "e-mail")->setcellvalue ("E1", "School")->setcellvalue ("F1", "Last Login Time"); $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 '); Generate Excel File Browser_export ("Excel5 "," Browser_excel03.xls "); Browser output $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 ');}
Run:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Phpexcel exporting MySQL database data