Export the contents of MySQL database to Excel we can simply implement the MySQL export Excel file by using the header output CSV format document, let's take a look at the example below.
The core code is here.
The code is as follows |
Copy Code |
Header ("Content-type:application/vnd.ms-excel; Charset=utf-8 "); Header ("Pragma:public"); Header ("expires:0"); Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0"); Header ("Content-type:application/force-download"); Header ("Content-type:application/octet-stream"); Header ("Content-type:application/download"); Header ("Content-disposition:attachment;filename=". $title. ". XLS "); Header ("Content-transfer-encoding:binary"); |
Here's how to use PHP to export the database to Excel and test it completely successfully
The code is as follows |
Copy Code |
$DB _server = "localhost"; $DB _username = "root"; $DB _password = www.bKjia.c0m; $DB _dbname = "Ishop"; $DB _tblname = "Oi_mall_payment"; $savename = Date ("Ymjhis"); $Connect = @mysql_connect ($DB _server, $DB _username, $DB _password) or Die ("couldn ' t Connect."); mysql_query ("Set Names ' GBK '"); $file _type = "Vnd.ms-excel"; $file _ending = "xls"; Header ("content-type:application/$file _type;charset=big5"); Header ("content-disposition:attachment; Filename= ". $savename.". $file _ending "); Header ("Pragma:no-cache"); $now _date = Date ("Y-m-j h:i:s"); $title = "Database name: $DB _dbname, Data sheet: $DB _tblname, Backup date: $now _date"; $sql = "SELECT * from $DB _tblname"; $ALT _db = @mysql_select_db ($DB _dbname, $Connect) or Die ("couldn ' t Select Database"); $result = @mysql_query ($sql, $Connect) or Die (Mysql_error ()); Echo ("$titlen"); $sep = "T"; for ($i = 0; $i < Mysql_num_fields ($result); $i + +) { Echo Mysql_field_name ($result, $i). "T"; } Print ("n"); $i = 0; while ($row = Mysql_fetch_row ($result)) { $schema _insert = ""; for ($j =0; $j if (!isset ($row [$j])) $schema _insert. = "NULL". $sep; ElseIf ($row [$j]! = "") $schema _insert. = "$row [$j]". $sep; Else $schema _insert. = "". $sep; } $schema _insert = Str_replace ($sep. " $ "," ", $schema _insert); $schema _insert. = "T"; Print (Trim ($schema _insert)); Print "n"; $i + +; } return (true); ?> |
http://www.bkjia.com/PHPjc/632912.html www.bkjia.com true http://www.bkjia.com/PHPjc/632912.html techarticle Export the contents of MySQL database to Excel we can simply implement the MySQL export Excel file by using the header output CSV format document, let's take a look at the example below. Core generation ...