首頁 > php教程 > php手册 > 主體

技巧分享 PHP批量导出csv文件

WBOY
發布: 2016-06-13 11:09:57
原創
957 人瀏覽過

对于

有时写程序时后台要求把大量数据导入数据库中,比如计算机考试成绩的查询、电话簿的数据等一般都是存放在excel中的,这时我们可把数据导出成csv文件,然后通过以下程序即可在后台批量导入数据到数据库中。

下面只是PHP批量导出csv文件的主要程序部分:

  1.  ?php  
  2. function getmicrotime(){  
  3. list($usec, $sec) = explode(” “,microtime());  
  4. return ((float)$usec + (float)$sec);  
  5. }  
  6. ?> 

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li>
<span>$</span><span class="attribute">time_start</span><span> = </span><span class="attribute-value">getmicrotime</span><span>();  </span>
</li>
<li class="alt"><span>include(”db.inc.php”);//连接数据库  </span></li>
<li>
<span>$</span><span class="attribute">db</span><span>=</span><span class="attribute-value">new</span><span> testcsv;  </span>
</li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
<li><span class="tag"><span> ?php  </span></span></li>
<li class="alt">
<span>$</span><span class="attribute">handle</span><span> = </span><span class="attribute-value">fopen</span><span> (”test.csv”,”r”);  </span>
</li>
<li>
<span>$</span><span class="attribute">sql</span><span>=”insert into scores(idcard,names<br>,num,sex,nation,score) values(’”;  </span>
</li>
<li class="alt">
<span>while ($</span><span class="attribute">data</span><span> = </span><span class="attribute-value">fgetcsv</span><span> ($handle, 1000, “,”)) {  </span>
</li>
<li>
<span>$</span><span class="attribute">num</span><span> = </span><span class="attribute-value">count</span><span> ($data);  </span>
</li>
<li class="alt">
<span>for ($</span><span class="attribute">c</span><span>=</span><span class="attribute-value">0</span><span>; $c </span><span class="tag"><span> $num; $c++) {  </span></span>
</li>
<li>
<span>if($</span><span class="attribute">c</span><span>==$num-1){$</span><span class="attribute">sql</span><span>=$sql.$data[$c].”‘)”;break;}  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">sql</span><span>=$sql.$data[$c].”‘,’”;  </span>
</li>
<li><span>}  </span></li>
<li class="alt">
<span>print “</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span>”;  </span></span>
</li>
<li>
<span>echo $sql.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span>”;  </span></span>
</li>
<li class="alt">
<span>$db-</span><span class="tag">></span><span>query($sql);  </span>
</li>
<li>
<span>echo “SQL语句执行成功!</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span>”;  </span></span>
</li>
<li class="alt">
<span>$</span><span class="attribute">sql</span><span>=”insert into scores(idcard,names<br>,num,sex,nation,score) values(’”;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>fclose ($handle);  </span></li>
<li>
<span>$</span><span class="attribute">time_end</span><span> = </span><span class="attribute-value">getmicrotime</span><span>();  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">time</span><span> = $time_end – $time_start;  </span>
</li>
<li><span>echo “程序执行时间:”.$time.”秒”;  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
登入後複製

希望大家通过以上PHP批量导出csv文件的代码示例能够掌握这一技巧。


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板