Home >php教程 >php手册 >php mysql 导出csv excel格式文件并保存

php mysql 导出csv excel格式文件并保存

WBOY
WBOYOriginal
2016-05-26 15:22:011679browse

这是一款我在自己用时写的一款利用php导出mysql数据库的数据保存成csv文件,并提供下载,原理很简单就是把mysql数据查出来,然后以csv格式保存到一个.csv文件这样就OK了,实例代码如下:

<?php
$times = time();
$filename = $times . ".csv";
$a = "联系人,联系电话,申请时间 ";
$days = postget("days");
$mktime = daystomktime($days);
$sql = "select * from  v_tugou where ($times-times)<$mktime";
$db = new db();
$result = $db->query($sql);
$rs = $db->fetch($result, 0);
foreach ($rs as $v => $vv) {
    $a.= $vv[&#39;name&#39;] . &#39;,&#39; . $vv[&#39;mo&#39;] . ",";
    $a.= date(&#39;y-m-d &#39;, $vv[&#39;times&#39;]) . " ";
} //开源代码phprm.com
//echo $a;
$hod = fopen($filename, "w+");
if (fwrite($hod, $a)) {
    echo "生成excel文件成功,点击<a href=$filename target=_blank>右击另存为excel文档</a>";
}

数据结构, 代码如下:

    --表的结构`hn_tugou`--createtableif notexists`v_tugou`(`id`int(4) notnullauto_increment, `name`varchar(20) notnull

default '0', `mo`varchar(20) notnull

default '0', `times`int(4)

default null, `ip`varchar(20) notnull

default '0', primarykey(`id`)) engine = myisam

default charset = utf8auto_increment = 2;

----导出表中的数据`hn_tugou`--insertinto`hn_tugou`(`id`, `name`, `mo`, `times`, `ip`) values(1, 'adsense', 'www.phprm.com', 1283241159, '192.168.0.119');


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn