Phpexcel exporting MySQL database data
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)
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:
0) {$objPHPExcel->createsheet ();} $objPHPExcel->setactivesheetindex ($i); $objSheet = $objPHPExcel->getactivesheet (); $data = $db->getuserinfo (); $objSheet->setcellvalue (A1, number)->setcellvalue (B1, login)->setcellvalue (C1, nickname)->setcellvalue (D1, email ->setcellvalue (E1, school)->setcellvalue (F1, last login time); $j = 2;foreach ($data as $key + $value) {# code ... $objSheet-& Gt;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:
http://www.bkjia.com/PHPjc/1047175.html www.bkjia.com true http://www.bkjia.com/PHPjc/1047175.html techarticle phpexcel export MySQL database data using Phpexcel Export file First look at the above article, write method, just the middle of the database operation, followed by the Excel browser output ...