东方星雨

简单网络

一个爱好网络的80后男站长。

关注我东方星雨个人微信号:476847113

您现在的位置是:首页 > 站长日志

如何利用php导出excel数据表

2018-10-14 站长 站长日志

利用php导出excel数据表代码

<?
Header("Content-type:application/octet-stream");
Header("Accept-Ranges:bytes");
Header("Content-type:application/vnd.ms-excel");  
Header("Content-Disposition:attachment;filename=test.xls");
$con = mysql_connect("localhost","数据库登陆名","数据库密码");
mysql_select_db("数据库名");
mysql_query('set names utf8');//数据库编码
$sql = "select 要输出的字段名,多个用逗号隔开 from 数据表单名";
$result = mysql_query($sql,$con);
echo "id\tusername";//输出的excel的列名,用\t换列
while ($rs=mysql_fetch_array($result)){
 echo "\n";//用\n换行
 echo $rs['输出字段名']."\t".$rs['输出字段名'];//用."\t".连接
}
?>

 

例子:

<?php
Header("Content-type:application/octet-stream");
Header("Accept-Ranges:bytes");
Header("Content-type:application/vnd.ms-excel");  
Header("Content-Disposition:attachment;filename=test.xls");
$con = mysql_connect("localhost","root","123456");
mysql_select_db("db_test");
mysql_query('set names utf8');
$sql = "select id,FirstName,LastName,Age,Hometown,Job from user";
$result = mysql_query($sql,$con);
echo "id\tFirstName\tLastName\tAge\tHometown\tJob";
while ($rs=mysql_fetch_array($result)){
 echo "\n";
 echo $rs['id']."\t".$rs['FirstName']."\t".$rs['LastName']."\t".$rs['Age']."\t".$rs['Hometown']."\t".$rs['Job'];
}
?>

文章评论