How to export database content to excel_PHP in php

Source: Internet
Author: User
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...

Related Article

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.