donglu1913 2014-10-02 08:57
浏览 23
已采纳

将100个Excel文件传输到MySQL

I use PHP dbase extention to read dbase files and upload them into MySQP.

My Code:

 if(dbase_open($FileDir,0)){

                    $dbOpen = dbase_open($FileDir,0);
                    $records = dbase_numrecords($dbOpen);

                    $qry = "";
                    $qry = "INSERT INTO ".$Table." (`SCADA`, `Date`, `PlantNo`, `State`, `SubState`, `Frequency`) VALUES ";

                    for ($i=1; $i<=$records; $i++){

                        $rows = dbase_get_record_with_names($dbOpen,$i);
                        $qry .= '('.$SCADA.','.$rows['Date'].','.$rows['PlantNo'].','.$rows['State'].','.$rows['SubState'].','.$rows['Frequency'].'),';

                    }

                    $qry = substr($qry, 0, -1);
                    $Insert = $conn -> prepare($qry);
                    $Insert -> execute();

Now I have to do the same but with xls files! I know that I can convert them into csv and import them via PHP, but I have 100 xls files... How can I transfer them to MySQL? Ant idea?

  • 写回答

3条回答 默认 最新

  • douhoujun9304 2014-10-02 09:18
    关注

    Direct import from Excel to MySQL will not be possible without a third party proprietary product, that you would need to buy like - dbForge Studio or something (I have not tried this one, so I cant even recommend it). Tool called phpmyadmin used to have this functionality, but its depreciated since version 3.4.5, so if you install an old one it might be an option for you. That really depends on several factors.

    For a general start, ofcourse, you should make sure that the file you want to import has the same columns, and column order as the database where you want to import it into.

    If you convert, or export your xls files into CSV (its a seperate story on how to do that and I will not cover here, plenty of resources or online converters out there), you can merge all of them either in Excel, or from your console. On all modern operating systems you can just put all files in the same directory, cd or dir into it and run "cat *.csv > merged.csv" which will merge all files into one. (copy /b *.csv merged.csv on windows machines)

    Once you have a single merged file you can run php to execute the mysql import - "LOAD DATA INFILE 'c:/path/to/file/merged.csv' INTO TABLE tablename" (more about it here - http://dev.mysql.com/doc/refman/5.1/en/load-data.html)

    Ofcourse you can also automate exporting XLS into CSV by using a php library like PHPExcel - https://phpexcel.codeplex.com/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)