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?

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样