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

php excel导入mysql: EXCEL导入mysql

WBOY
WBOY원래의
2016-06-21 08:49:31871검색
<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으로 문의하세요.