PHP导出数据到CSV文件实例代码如下:query($sql);
$rs = $db->fetch($result, 0);
foreach ($rs as $v => $vv) {
$a.= $vv['name'] . ',' . $vv['mo'] . "," . $vv['comm'] . "," . commbox($vv['cbox']) . ",";
$a.= "" . $vv['bak'] . "," . date('y-m-d', $vv['times']) . "
";
}
//echo $a;
$hod = fopen($filename, "w+");
if (fwrite($hod, $a)) {
echo "生成文件成功,点击下载excel
文档";
}
function daystomktime($days) {
switch ($days) {
case 1:
$t = 86400;
break;
case 7:
$t = 86400 * 7;
break;
case 30:
$t = 86400 * 30;
break;
case 90:
$t = 86400 * 90;
break;
case 2048:
$t = 86400 * 2048;
break;
}
return $t;
}
function commbox($ars) {
if (trim($ars) == '') {
return ' ';
}
$array = include ("tugou.php");
$tmp = explode(',', $ars);
$str = '';
if ($tmp) {
foreach ($tmp as $c) {
$str.= $array[intval($c) ] . '|';
}
}
return $str;
}
?>
数据库的结构代码如下:
create table if not exists `tugou` (
`id` int(8) not null auto_increment,
`name` varchar(20) default null comment '名字',
`mo` varchar(20) default null comment '手机',
`comm` varchar(100) default null,
`bak` varchar(500) default null comment '备份',
`cbox` varchar(500) default null comment '类型',
`times` int(4) default null,
`ip` varchar(20) default null,//开源代码phprm.com
primary key (`id`)
) engine=innodb default charset=utf8 auto_increment=8 ;
本文地址:
转载随意,但请附上文章地址:-)