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"); |
The following is to export the database to Excel with PHP, the test is completely successful
| The code is as follows |
Copy Code |
|
<?php
$DB _server = "localhost";
$DB _username = "root";
$DB _password = www.111cn.net;
$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, Datasheet: $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 <mysql_num_fields ($result); $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);
?> |