您的位置:首页 > 数据库

PHPExcel下载(从数据库获取数据)示例代码

2015-04-28 11:28 441 查看
<?php

function exportExcel($filename,$content){

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Content-Type: application/vnd.ms-execl");

header("Content-Type: application/force-download");

header("Content-Type: application/download");

header("Content-Disposition: attachment; filename=".$filename);

header("Content-Transfer-Encoding: binary");

header("Pragma: no-cache");

header("Expires: 0");

echo $content;

}

$dsn="mysql:dbname=ci;host=localhost";

$db=new PDO($dsn,'root','root',array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\''));//实例化pdo对象

$res=$db->query("select * from student");//从数据库查询数据

$str = "姓名\t性别\t年龄\t\n";

$str = iconv('utf-8','gb2312',$str);//iconv() 是编码转换函数

while($row=$res->fetchAll()){

$name = iconv('utf-8','gb2312',$row['name']);

$sex = iconv('utf-8','gb2312',$row['sex']);

$str .= $name."\t".$sex."\t".$row['age']."\t\n";

}

$filename = date('Ymd').'.xls';

exportExcel($filename,$str); //生成excel表格
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐