PHP导出数据到Excel

<?php

date_default_timezone_set(‘PRC‘);

$filename="info.xls";//先定义一个excel文件

header("Content-Type: application/vnd.ms-execl");
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");

//我们先在excel输出表头,当然这不是必须的
echo iconv("utf-8", "gb2312", "编号")."\t";
echo iconv("utf-8", "gb2312", "姓名")."\t";
echo iconv("utf-8", "gb2312", "年龄")."\t";
echo iconv("utf-8", "gb2312", "性别")."\n";//注意这个要换行

//这里我们定义一个数据库为datebse 数据库用户名:root 密码为:123456
$conn = mysql_connect("localhost","root","");
mysql_select_db("thinkphp");
mysql_query("set names utf8");
//在这里我们定义一个名叫studen的表,她有id,name,age,sex四个字段
$sql="select id,name,age,sex from student";
$result=mysql_query($sql);
while($row =mysql_fetch_array($result)){
    echo iconv("utf-8", "gb2312", $row[‘id‘])."\t";
    echo iconv("utf-8", "gb2312", $row[‘name‘])."\t";
    echo iconv("utf-8", "gb2312", $row[‘age‘])."\t";
    echo iconv("utf-8", "gb2312", $row[‘sex‘])."\n";
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。