php导出excel表格

来源:互联网 发布:淘宝店开通花呗 编辑:程序博客网 时间:2024/04/20 08:58
<?php
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename=demo.xls');
header('Pragma: no-cache');
header('Expires: 0');
 
$title = array('编号', '姓名', '性别', '年龄', '身高', '体重');
$data = array(
array(1, '张三', '男', '22', 183, 72),
array(2, '李四', '女', '18', 170, 50),
array(3, '王五', '男', '14', 178, 68),
array(4, '赵六', '女', '34', 163, 48)
);
echo iconv('utf-8', 'gbk', implode("\t", $title)), "\n";
foreach ($data as $value) {
echo iconv('utf-8', 'gbk', implode("\t", $value)), "\n";
}
?>

简单纯粹的php导出excel表格方法,木有过多的解释,花哨的装饰,一看就懂。
[php] header(“Content-type:application/vnd.ms-excel”);
header(“Content-Disposition:attachment; filename=用户信息表.xls”);
require_once(‘./class.db.php’);
$db = new DB(‘localhost’, ‘app_vmovies’, ‘root’, ‘123456’);
$sql = ‘select * from `user` order by `id` desc';
$ret = $db->get_results($sql);

// 输出内容如下:
echo “ID”.”\t”;
echo “昵称”.”\t”;
echo “性别”.”\t”;
echo “OpenId”.”\t”;
echo “平台”.”\t”;
echo “注册日期”.”\t”;
echo “\n”;

$len = count($ret);
for ( $i = 0; $i < $len; $i++ ) {
echo $ret[$i][“id”].”\t”;
echo $ret[$i][“nickname”].”\t”;
echo $ret[$i][“gender”].”\t”;
echo $ret[$i][“openid”].”\t”;
echo $ret[$i][“pf”].”\t”;
echo $ret[$i][“regdate”].”\t”;
echo “\n”;
}
[/php]

 

0 0
原创粉丝点击