douzuan5365
2010-09-25 20:32
浏览 37
已采纳

如何在php中将.xls文件加载到mysql数据库?

I want the exact opposite of this.

I want to load a .xls file full of emails into a database column.

What's the simplest way to do this?

图片转代码服务由CSDN问答提供 功能建议

我想与

我想将一个充满电子邮件的.xls文件加载到数据库列中。

最简单的方法是什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douzi1350 2010-09-25 21:55
    已采纳

    Like Ponies said, you will have to first "Save As > CSV" then upload and do whatever. Often permissions will not allow that SQL statement however. My personal favorite is using csv2sql.com because it will do the heavy lifting and give you the SQL to just execute, however you could use fgetcsv which would be more secure, and something you can implement into a script.

    With fgetcsv, you could do something like this assuming you just had a one column file with the email addresses listed:

    $row = 1;
    if (($handle = fopen("test.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            //echo "<p> $num fields in line $row: <br /></p>
    ";
            $row++;
            for ($c=0; $c < $num; $c++) {
                if ($c==0){
                     //This is the first column
                     //Just run a generic MySQL insert command here (see below for link for more info on this)
                }
            }
        }
        fclose($handle);
    }
    

    Tutorial on PHP MySQL Insert: at W3Schools. That should get you going on this endeavor.

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题