借鉴了一个朋友的思路:http://mxp556.blog.hexun.com/23315689_d.html;
简单思路如下:
1、将excel文件另存为txt文件。
2、用fopen()读取文件内容。
3、分析文件内容的结构,构成sql语句。
4、执行sql语句。
ok,下面开始举例。(最重要的是掌握思路!)
假设有一个test.xls的excel文件存储着我们要导入的内容。内容如下:

首先将test.xls文件另存为test.txt文件(注意:要选择制表符分割的那个.txt)

,中间可能会有什么提示,不管它,直接点“确定”或者“是”。然后打开test.txt文件,将它的编码另存为utf-8或者是gb2312,最主要是要跟下面的编码统一,以防出现乱码。
<?php
header("Content-type:text/html;charset=utf-8"); //强制页面以utf-8编码显示,跟前面test.txt文件的编码要一致。
$conn=@mysql_connect("localhost","root","123456") or die("连接数据失败"); //连接数据库.
@mysql_select_db("test",$conn) or die("打开数据库失败"); //选择数据库,自己创建,还有表也是自己先建好来。
mysql_query("set names utf8");//跟上面的编码要统一,还有要注意的是utf8,不是utf-8。
$file=@fopen("test.txt",'r') or die("打开文件失败"); // 以只读方式打开test.txt文件。
//我们打开test.txt文件,可以看到test.txt文件内容的每一行与test.xls文件的每一行相对应。
//也就是说除了第一行外,每一行就相当于表的一条记录,我们只要循环插入每一行数据到表中就行了。
fgets($file); //先读取文件的第一行,因为第一行不是我们要的内容。
//下面循环读取每一行,第一行已经读取了,所以从第二行开始读取。
while(!feof($file)){
$val=fgets($file);
//下面一步很关键,用explode()将每一行转换为数组来存储每个字段的值。
//比如说第二行的内容是:1 陈勇文 男 电子商务;
//转换为数组就变成array(1,"陈勇文","男","电子商务");这对于我们插入数据库就很有帮助了。
$values=explode("\t",trim($val)); //注意用来分割的是制表符"\t",而不是空格" " ,自己也可以输出来测试一下。
$sql="insert into `test`(`id`,`name`,`sex`,`major`) values('$values[0]','$values[1]','$values[2]','$values[3]')";
if(!empty($values[0])) mysql_query($sql); //因为最后一行可能是空数据,所以做了个判断。
}
fclose($file);
?>搞定,收工。有问题欢迎提出。
//经一个朋友提醒,将excel文件转换为csv格式更好,然后用fetcsv()读取每一行,这样可以直接把每一行转换为数组,不用explode()分割了。
« 上一篇:采集---强大的“小偷” php+mysql+thinkphp学生选课系统:下一篇 »
发表于 2012年02月4日 7:53 下午
My brother suggested I might like this blog. He was totally right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!
[回复]
发表于 2012年02月4日 8:44 下午
I got what you designate, thanks for putting up. Woh I am glad to grow this website finished google. Thanks For Share php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡.
[回复]
发表于 2012年02月4日 9:28 下午
An gripping language is worth account. I expect that you should make author on this substance, it power not be a bias subordinate but mostly group are not sufficiency to verbalise on specified topics. To the next. Cheers like your php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡.
[回复]
发表于 2012年02月4日 9:41 下午
Thanks for taking the time to discuss this subject. I really appreciate it. I’ll stick a link of this entry in my blog.
[回复]
发表于 2012年02月4日 10:10 下午
Excellent goods from you, man. php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ I've understand your stuff previous to and you are just extremely excellent. I actually like what you have acquired here, really like what you are saying and the way in which you say it. You make it enjoyable and you still care for to keep it wise. I cant wait to read much more from you. This is really a wonderful php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ informations.
[回复]
发表于 2012年02月4日 10:18 下午
php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ I was recommended this web site by my cousin. I'm not sure whether this post is written by him as nobody else know such detailed about my problem. You are wonderful! Thanks! your article about php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡Best Regards Lawrence
[回复]
发表于 2012年02月4日 11:19 下午
Hey there, I think your website might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, great blog!
[回复]
发表于 2012年02月4日 11:46 下午
Hello There. I found your blog using msn. This is a really well written article. I'll be sure to bookmark it and come back to read more of php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ . Thanks for the post. I will definitely return.
[回复]
发表于 2012年02月5日 12:31 上午
Great - I ought to undoubtedly pronounce, impressed with your site. I had no trouble navigating by means of all the tabs and related information ended up being truly effortless to do to access. I lately discovered what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for people who add forums or anything, web internet site theme . a tones way for your customer to communicate. Good task.
[回复]
发表于 2012年02月5日 1:07 上午
Of course even if, various institutions as well as businesses prepare compatible with some others.
[回复]
发表于 2012年02月5日 2:05 上午
An gripping communicating is designer notice. I conceive that you should pen solon on this subject, it might not be a taboo thing but generally grouping are not enough to talk on much topics. To the next. Cheers like your php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡.
[回复]
发表于 2012年02月5日 2:15 上午
Perfect just what I was searching for! .
[回复]
发表于 2012年02月5日 3:17 上午
Wonderful goods from you, man. php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ I've understand your stuff previous to and you are just extremely fantastic. I really like what you have acquired here, really like what you're saying and the way in which you say it. You make it enjoyable and you still take care of to keep it smart. I can not wait to read much more from you. This is actually a great php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ informations.
[回复]
发表于 2012年02月5日 7:19 上午
This is very interesting, You're a very skilled blogger. I've joined your feed and look forward to seeking more of your fantastic post. Also, I've shared your website in my social networks!
[回复]
发表于 2012年02月5日 7:40 上午
Glad to be 1 of numerous visitants on this remarkable internet internet site : D.
[回复]
发表于 2012年02月5日 8:08 上午
i cant get how you'll be able to share like this incredible posts admin very much thanks
[回复]
发表于 2012年02月5日 9:05 上午
Uh, well, explain me a please, I am not quite in the subject, how can it be?!...
[回复]
发表于 2012年02月5日 9:16 上午
Wow, awesome weblog structure! How long have you ever been running a blog for? you make running a blog glance easy. The entire glance of your web site is fantastic, as neatly as the content material!
[回复]
发表于 2012年02月5日 1:19 下午
Thanks a lot for giving your opinions. Being a blogger, I'm constantly hunting for new and different approaches to look at a matter. I get fantastic motivation in doing so. Thanks a ton again
[回复]
发表于 2012年02月5日 2:39 下午
Does your site have a contact page? I'm having trouble locating it but, I'd like to shoot you an e-mail. I've got some suggestions for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it expand over time.
[回复]
发表于 2012年02月5日 6:37 下午
Hello there, simply turned into aware about http://www.googlephp.cn/archives/148 thru Yahoo and google, and found that propose being very careful informative. I'm gonna watch apart for brussels. I will appreciate if you happen to continue this in upcoming. A lot of people will be benefited out of your writing. Cheers! roksa
[回复]
发表于 2012年02月5日 7:54 下午
Wonderful blog. Plenty of helpful information here. I was looking for this.Thanks for sharing!
[回复]
发表于 2012年02月5日 8:02 下午
php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You are wonderful! Thanks! your article about php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡Best Regards Lawrence
[回复]
发表于 2012年02月5日 8:15 下午
php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ I was suggested this website by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You're wonderful! Thanks! your article about php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ Best Regards Justin Lisa
[回复]
发表于 2012年02月5日 8:15 下午
php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ I was recommended this blog by my cousin. I'm not sure whether this post is written by him as no one else know such detailed about my problem. You are incredible! Thanks! your article about php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ Best Regards Cassetta Shane
[回复]
发表于 2012年02月5日 8:54 下午
you are in point of fact a good webmaster. The web internet site loading pace is incredible. It seems that you are doing any distinctive trick. Also, The contents are masterpiece. you’ve done a outstanding job in this subject!
[回复]
发表于 2012年02月5日 9:45 下午
It's a shame you don't have a donate button! I'd definitely donate to this superb blog! I suppose for now i'll settle for book-marking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this site with my Facebook group. Chat soon!
[回复]
发表于 2012年02月5日 10:17 下午
php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ I was suggested this website by my cousin. I'm not sure whether this post is written by him as no one else know such detailed about my problem. You are incredible! Thanks! your article about php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡Best Regards Shane
[回复]
发表于 2012年02月5日 10:27 下午
you are really a good webmaster. The web site loading speed is amazing. It seems that you are doing any unique trick. Also, The contents are masterpiece. you have done a wonderful job on this topic!
[回复]
发表于 2012年02月5日 11:52 下午
Magnificent goods from you, man. php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ I've understand your stuff previous to and you are just extremely wonderful. I really like what you've acquired here, really like what you're saying and the way in which you say it. You make it entertaining and you still take care of to keep it smart. I cant wait to read far more from you. This is really a great php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ informations.
[回复]
发表于 2012年02月6日 12:33 上午
Vi skrev denna guiden med mål att guida konsumenter utvälja billiga och bra webbhotell och samtidigt slippa problem i framtiden. Vissa webbhotell är riktigt bra samtidigt som andra kan orsaka till med onödiga problem för dig. Ett flertal använder dålig reklam för att locka till kunder.
[回复]
发表于 2012年02月6日 1:21 上午
Hey There. I found your blog using msn. This is a very well written article. I'll make sure to bookmark it and come back to read more of php简å•实现将excelæ–‡ä»¶çš„æ•°æ®æ‰¹é‡å¯¼å…¥åˆ°æ•°æ®åº“ | 陈勇文的åšå®¢-专注于php+mysql程åºè®¾è®¡ . Thanks for the post. I will definitely comeback.
[回复]
发表于 2012年02月6日 2:23 上午
It’s actually a great and helpful piece of info. I’m glad that you shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.
[回复]
发表于 2012年02月6日 3:04 上午
Very nice post. I simply stumbled upon your weblog and wished to say that I've truly loved surfing around your weblog posts. After all I will be subscribing to your rss feed and I am hoping you write once more very soon!
[回复]
发表于 2012年02月6日 4:41 上午
I'm glad to be a visitor of this consummate web internet site! , appreciate it for this rare info ! .
[回复]