Home >Backend Development >PHP Tutorial >PHP export csv or xls file program_PHP tutorial
Due to work needs, today we have to export the data in the mysql database to execel for use. Later, I searched on the Internet and found out that it is so simple, because the csv file only needs to be separated by ",". Next we will Let’s take a look at an example.
include("../inc/connect.php");
$Date = date("Y-m-d");
$Filename = $Date.".csv";
$Table = isset($_GET['tid'])?$_GET['tid']:'';
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=".$Filename);
echo "Username, real name, contact information, contact QQ, contact address, email address rn";
$Sql = "Select * from gx_member where datediff(m_time,'".date("Y-m-d H:i:s")."')<=$Table order by id desc ";
$Result = mysql_query($Sql);
while($rs = mysql_fetch_array($Result) ){
echo $rs['m_name'].','. $rs['m_realname'].",". $rs['m_tel'].",". $rs['m_qq'].",". $ rs['m_address'].",". $rs['m_mail']." rn ";
}
?>
What we used above is to export csv files. There is another way to export xls files. Just modify it
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=".$Filename);
That’s it.
Please indicate www.111cn.cn/phper/php.html for original reprints on this site