首頁 >php教程 >php手册 >php excel导入mysql: EXCEL导入mysql

php excel导入mysql: EXCEL导入mysql

WBOY
WBOY原創
2016-06-21 08:49:31872瀏覽
<ol class="dp-c">
<li class="alt"><span><span class="keyword">require_once</span><span> </span><span class="string">'phpexcel/Classes/PHPExcel/IOFactory.php'</span><span>; </span></span></li>
<li><span><span class="keyword">require_once</span><span> </span><span class="string">'phpexcel/Classes/PHPExcel/Reader/Excel5.php'</span><span>; </span></span></li>
<li class="alt"><span><span class="vars">$objReader</span><span> = PHPExcel_IOFactory::createReader(</span><span class="string">'Excel5'</span><span>);</span><span class="comment">//use excel2007 for 2007 format </span><span> </span></span></li>
<li><span><span class="vars">$objPHPExcel</span><span> = </span><span class="vars">$objReader</span><span>->load(</span><span class="vars">$filename</span><span>);   </span><span class="comment">//$filename可以是上传的文件,或者是指定的文件</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$sheet</span><span> = </span><span class="vars">$objPHPExcel</span><span>->getSheet(0);  </span></span></li>
<li><span><span class="vars">$highestRow</span><span> = </span><span class="vars">$sheet</span><span>->getHighestRow(); </span><span class="comment">// 取得总行数 </span><span> </span></span></li>
<li class="alt"><span><span class="vars">$highestColumn</span><span> = </span><span class="vars">$sheet</span><span>->getHighestColumn(); </span><span class="comment">// 取得总列数</span><span> </span></span></li>
<li><span><span class="vars">$k</span><span> = 0;    </span></span></li>
<li class="alt"><span><span class="comment">//循环读取excel文件,读取一条,插入一条</span><span> </span></span></li>
<li><span>   <span class="keyword">for</span><span>(</span><span class="vars">$j</span><span>=2;</span><span class="vars">$j</span><span><span class="vars">$highestRow</span><span>;</span><span class="vars">$j</span><span>++) </span></span></span></li>
<li class="alt"><span>   { </span></li>
<li><span>     <span class="vars">$a</span><span> = </span><span class="vars">$objPHPExcel</span><span>->getActiveSheet()->getCell(</span><span class="string">"A"</span><span>.</span><span class="vars">$j</span><span>)->getValue();</span><span class="comment">//获取A列的值</span><span> </span></span></li>
<li class="alt"><span>     <span class="vars">$b</span><span> = </span><span class="vars">$objPHPExcel</span><span>->getActiveSheet()->getCell(</span><span class="string">"B"</span><span>.</span><span class="vars">$j</span><span>)->getValue();</span><span class="comment">//获取B列的值</span><span> </span></span></li>
<li><span>     <span class="vars">$sql</span><span> = </span><span class="string">"INSERT INTO table VALUES("</span><span>.</span><span class="vars">$a</span><span>.</span><span class="string">","</span><span>.</span><span class="vars">$b</span><span>.</span><span class="string">")"</span><span>; </span></span></li>
<li class="alt"><span>     mysql_query(<span class="vars">$sql</span><span>); </span></span></li>
<li><span>   } </span></li>
</ol>
本文链接http://www.cxybl.com/html/wlbc/Php/20130701/38844.html



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn