Php exports a data table as an Excel table in the simplest and fastest way (no plug-ins required ). First, define the header information to output an excel file. Then, echo the database information in the form of a table. Copy the code as follows :? Phpheader (Content-first defines the header information, indicating that an excel file is output. Then, echo the database information in the form of a table.
The code is as follows:
Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: filename=xls_region.xls ");
$ Pai_dbhost = 'localhost ';
$ Pai_dbname = 'testdb ';
$ User_dbuser = 'root ';
$ Mongo_dbpwd = 'root ';
$ Pai_db_language = 'utf8 ';
// END configuration
// Link to the database
$ Link = mysql_connect ($ pai_dbhost, $ pai_dbuser, $ pai_dbpwd );
Mysql_select_db ($ pai_dbname );
// Select encoding
Mysql_query ("set names". $ pai_db_language );
// Users table
$ SQL = "desc users ";
$ Res = mysql_query ($ SQL );
Echo"
";// Export the header (that is, the fields in the table)While ($ row = mysql_fetch_array ($ res )){$ T_field [] = $ row ['field']; // F in Field must be capitalized; otherwise, no result is returned.Echo"
". $ Row ['field']." | ";}Echo"
";// Export 100 data records$ SQL = "select * from users' limit 100 ";$ Res = mysql_query ($ SQL );While ($ row = mysql_fetch_array ($ res )){Echo"
";Foreach ($ t_field as $ f_key ){Echo"
". $ Row [$ f_key]." | ";}Echo"
";}Echo"
";
?>
Bytes. Then, echo the database information in the form of a table. The code is as follows :? Php header ("Content -...