Phpexcel exporting MySQL database data _php tutorial

Source: Internet
Author: User

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.