Heim  >  Artikel  >  Backend-Entwicklung  >  技巧分享 PHP批量导出csv文件_PHP教程

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

WBOY
WBOYOriginal
2016-07-15 13:33:14750Durchsuche

对于

有时写程序时后台要求把大量数据导入数据库中,比如计算机考试成绩的查询、电话簿的数据等一般都是存放在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文件的代码示例能够掌握这一技巧。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446090.htmlTechArticle对于 有时写程序时后台要求把大量数据导入数据库中,比如计算机考试成绩的查询、电话簿的数据等一般都是存放在excel中的,这时我们可...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn