Home > php教程 > PHP源码 > body text

php快速导出csv格式数据程序代码

WBOY
Release: 2016-06-08 17:22:05
Original
1069 people have browsed it

导出csv数据很简单因为csv格式的数据就是一个文本类型了,我们要导入到只要以,号分开它们数据就可以了,然后再利用header输入csv格式或者excel格式就可以了。

<script>ec(2);</script>

csv文件格式

 代码如下 复制代码

$exportdata = '规则111,有效期'."\n";

csv文件在php输出需要使用header告诉浏览器格式

 代码如下 复制代码

header("Content-type:application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename");

例子

 代码如下 复制代码

$exportdata = '规则111,规则222,审222,规222,服2222,规则1,规则2,规则3,匹配字符,设置时间,有效期'."\n";
$date = date("YmdHis");
if (!empty($lists)){
    foreach($lists as $key => $value){
      $time = date("Y-m-d_H:i:s", $value['add_time']);
      $exportdata .= "\"\t".$value['Rule_id']."\",\"\t".$value['Rule_name']."\",\"\t".$value['Matching_level']."\",\"\t"."{$value['Rule_action']}"."\",\"\t".$value['Service_type']."\",\"\t".$value['Keyword1']."\",\"\t".$value['Keyword2']."\",\"\t".$value['Keyword3']."\",\"\t".$value['Matching_word']."\",\"\t".$value['Set_time']."\",\"\t".$value['Validation_time']."\"\n";
    }
}
$filename = "plcnetinfo_{$date}.csv";
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename");
header("Expires: 0");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
     
echo(mb_convert_encoding($exportdata,"gb2312","UTF-8"));

下面再整理了一个php+mysql导入csv数据的例子

 代码如下 复制代码

export_csv();
function export_csv() {
$filename = date('YmdHis').".csv";//文件名
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo array_to_string(get_export_data());
}
function array_to_string($result) {
if(empty($result)) {
return i("没有符合您要求的数据!^_^");
}
$data = '书ID,书名'."\n"; //栏目名称
$size_result = sizeof($result);
for($i = 0 ; $i $data .= i($result[$i]['name']).','.i($result[$i]['option'])."\n";
}
return $data;
}
function get_export_data() {
$link = mysql_connect('localhost','root','121051xz') or die(mysql_error());
mysql_select_db('ht');
mysql_query("set names 'utf8'");//定义编码
$sql = 'select * from booklist';
$result = mysql_query($sql);
$rowaa = mysql_fetch_array($result);
$res = array();
$i = 0;
while($row = mysql_fetch_array($result)) {
$res[$i]['name'] = $row['bookid'];
$res[$i]['option'] = $row['bookname'];
$i++;
}
return $res;
}
function i($strInput) {
return iconv('utf-8','gb2312',$strInput);//页面编码为utf-8时使用,否则导出的中文为乱码
}

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!