Maison > php教程 > php手册 > le corps du texte

php excel导入mysql: EXCEL导入mysql

WBOY
Libérer: 2016-06-21 08:49:31
original
860 Les gens l'ont consulté
<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>
Copier après la connexion
本文链接http://www.cxybl.com/html/wlbc/Php/20130701/38844.html



Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!