码迷,mamicode.com
首页 > Web开发 > 详细

PHP导出数据到Excel

时间:2014-10-17 15:14:12      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   sp   文件   数据   div   on   

<?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";
}

 

PHP导出数据到Excel

标签:blog   io   os   ar   sp   文件   数据   div   on   

原文地址:http://www.cnblogs.com/qisel/p/4031093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!