Export the database content to excel in php. To export the content in the mysql database to excel, you only need to use the header to output the csv format document, you can simply export the excel file in mysql. let's take a look at the example below. The core generation exports the content in the mysql database to excel. we only need to use the header to output the csv format document, and we can simply export the excel file in mysql. let's take a look at the example below.
The core code is here.
The code is as follows: |
|
Header ("Content-Type: application/vnd. ms-excel; charsets = 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 "); |
The following shows how to export the database to excel using php. the test is successful.
The code is as follows: |
|
$ 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 Table: $ 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 ); ?> |
Bytes. Core generation...