The easiest and fastest way for php to export a data table to an Excel table (without plug ins)
- 2021-06-28 08:48:22
- OfStack
The header information is defined to indicate that an excel is output.Then, in the form of table, you can loop the information of the database out of echo.
<?php
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 To configure
// 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 surface
$sql = "desc users";
$res = mysql_query($sql);
echo "<table><tr>";
// Export table headers (that is, fields owned by the table)
while($row = mysql_fetch_array($res)){
$t_field[] = $row['Field']; //Field In F Capitalize, otherwise no results
echo "<th>".$row['Field']."</th>";
}
echo "</tr>";
// export 100 Bar data
$sql = "select * from users limit 100";
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)){
echo "<tr>";
foreach($t_field as $f_key){
echo "<td>".$row[$f_key]."</td>";
}
echo "</tr>";
}
echo "</table>";
?>