PHP简单excel导入数据库实例

先准备一个excle:2009.xls 打开它另存为csv格式,一定是打开另存为!

<?php 
header("Content-Type:text/html;charset=gbk"); 

$conn=mysql_connect('localhost','root','123456')or die("数据库连接失败"); 
mysql_select_db('test',$conn); 
mysql_query("SET NAMES 'GB2312'"); 

$filename="2009.csv"; //另存为的csv文件 
$fp = fopen($filename,"r"); 
    while (!feof($fp)){ 
          $arr=fgetcsv($fp);//获取一行的信息 
          $sql="INSERT INTO `test`.`exl` (`id`, `name`, `tel`, `add`) VALUES (NULL, '".$arr[0]."', '".$arr[1]."', '".$arr[2]."');"; 
        mysql_query($sql);
         //echo $sql; 
     } 
fclose($fp ); 
?>

 提示:test数据库里面的exl表是需要先做好的

 相关文章:

关于fgetcsv中文乱码问题

方便打开csv的软件wpsoffice:http://www.wps.cn/

posted @ 2012-11-28 21:10  tinyphp  Views(513)  Comments(0Edit  收藏  举报