Home  >  Article  >  Backend Development  >  The simplest and fastest way to export a data table to an Excel table in PHP (without plug-ins)_PHP Tutorial

The simplest and fastest way to export a data table to an Excel table in PHP (without plug-ins)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:57902browse

First define the header information, which means outputting an excel. Then the database information is echoed out in a table format in a loop, and that's it.

Copy code The code is as follows:


header("Content-type:application /vnd.ms-excel");
header("Content-Disposition:filename=xls_region.xls");

$cfg_dbhost = 'localhost';
$cfg_dbname = 'testdb';
$cfg_dbuser = 'root';
$cfg_dbpwd = 'root';
$cfg_db_language = 'utf8';
// END configuration

//Link database
$link = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
mysql_select_db($cfg_dbname);
//Select encoding
mysql_query("set names " .$cfg_db_language);

//users table
$sql = "desc users";

$res = mysql_query($sql);
echo "

";
//Export the table header (that is, the fields owned in the table)
while($row = mysql_fetch_array($res)){
$t_field[] = $row[' Field']; //The F in Field must be capitalized, otherwise there will be no result
echo "";
}
echo "";
//Export 100 pieces of data
$sql = "select * from users limit 100";
$res = mysql_query($sql);
while( $row = mysql_fetch_array($res)){
echo "";
foreach($t_field as $f_key){
echo "";
}
echo "";
}
echo "
".$row['Field']."
".$row[$f_key] ."
";

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/768137.htmlTechArticleFirst define the header information, which means outputting an excel. Then the database information is echoed out in a loop in the form of a table, and that's it. Copy the code. The code is as follows: ?php header("Content-...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn