index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>使用PHP导入和导出数据为CSV文件</title>
<link rel="stylesheet" type="text/css" href="http://skill86.blog.163.com/blog/../css/main.css" />
<style type="text/css">
.demo{400px; height:100px; margin:100px auto}
.demo p{line-height:32px}
.btn{80px; height:26px; line-height:26px; background:url(btn_bg.gif) repeat-x; border:1px solid #ddd; cursor:pointer}
</style>
</head>
<body>
<div id="header">
<div id="logo"><h1><a href="http://www.helloweba.com"
title="返回helloweba首页">helloweba</a></h1></div>
</div>
<div id="main">
<h2 class="top_title"><a
href="http://www.helloweba.com/view-blog-171.html">使用PHP导入和导出数据为CSV文
件</a></h2>
<div class="demo">
<form id="addform" action="do.php?action=import" method="post" enctype="multipart/form-data">
<p>请选择要导入的CSV文件:<br/><input type="file" name="file">
<input type="submit" class="btn" value="导入CSV">
<input type="button" class="btn" id="exportCSV" value="导出CSV"
onClick="window.location.href='http://skill86.blog.163.com/blog/do.php?action=export'"></p>
</form>
</div>
</div>
<div id="footer">
<p>Powered by helloweba.com 允许转载、修改和使用本站的DEMO,但请注明出处:<a
href="http://www.helloweba.com">www.helloweba.com</a></p>
</div>
<p
id="stat"><script type="text/javascript"
src="http://js.tongji.linezing.com/1870888/tongji.js"></script></p>
</body>
</html>
do.php
<?php
/**
* @
* @Description:
* @Copyright (C) 2011 helloweba.com,All Rights Reserved.
* -----------------------------------------------------------------------------
* @author: Liurenfei (lrfbeyond@163.com)
* @Create: 2012-5-1
* @Modify:
*/
include_once ("connect.php");
$action = $_GET['action'];
if ($action == 'import') { //导入CSV
$filename = $_FILES['file']['tmp_name'];
if (empty ($filename)) {
echo '请选择要导入的CSV文件!';
exit;
}
$handle = fopen($filename, 'r');
$result = input_csv($handle); //解析csv
$len_result = count($result);
if($len_result==0){
echo '没有任何数据!';
exit;
}
for ($i = 1; $i < $len_result; $i++) { //循环获取各字段值
$name = iconv('gb2312', 'utf-8', $result[$i][0]); //中文转码
$sex = iconv('gb2312', 'utf-8', $result[$i][1]);
$age = $result[$i][2];
$data_values .= "('$name','$sex','$age'),";
}
$data_values = substr($data_values,0,-1); //去掉最后一个逗号
fclose($handle); //关闭指针
$query = mysql_query("insert into student (name,sex,age) values $data_values");//批量插入数据表中
if($query){
echo '导入成功!';
}else{
echo '导入失败!';
}
} elseif ($action=='export') { //导出CSV
$result = mysql_query("select * from student");
$str = "姓名,性别,年龄
";
$str = iconv('utf-8','gb2312',$str);
while($row=mysql_fetch_array($result)){
$name = iconv('utf-8','gb2312',$row['name']);
$sex = iconv('utf-8','gb2312',$row['sex']);
$str .= $name.",".$sex.",".$row['age']."
";
}
$filename = date('Ymd').'.csv';
export_csv($filename,$str);
}
function input_csv($handle) {
$out = array ();
$n = 0;
while ($data = fgetcsv($handle, 10000)) {
$num = count($data);
for ($i = 0; $i < $num; $i++) {
$out[$n][$i] = $data[$i];
}
$n++;
}
return $out;
}
function export_csv($filename,$data) {
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 $data;
}
?>
//链接数据库
connect.php
<?php
$host="localhost";
$db_user="root";
$db_pass="";
$db_name="demo";
$timezone="Asia/Shanghai";
$link=mysql_connect($host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
mysql_query("SET names UTF8");
header("Content-Type: text/html; charset=utf-8");
?>