PHP to export the contents of the database to the excel_php tutorial

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

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