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

php excel导入mysql: EXCEL导入mysql

WBOY
WBOYOriginal
2016-06-21 08:49:31874browse
<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



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn