Home > php教程 > php手册 > 如何用phpExcel将Excel导入到Mysql数据库

如何用phpExcel将Excel导入到Mysql数据库

WBOY
Release: 2018-10-25 17:16:54
forward
1561 people have browsed it

这篇文章主要介绍了如何用phpExcel将Excel导入到Mysql数据库 ,有一定的参考价值,感兴趣的朋友可以看看。

1、http://phpexcel.codeplex.com/下载phpExcel

2、代码如下:

<?require_once &#39;PHPExcel.php&#39;;require_once &#39;PHPExcel/IOFactory.php&#39;;require_once &#39;PHPExcel/Reader/Excel5.php&#39;;
//以上三步加载phpExcel的类$objReader = PHPExcel_IOFactory::createReader(&#39;Excel5&#39;);
//use excel2007 for 2007 format $filename="jxlrwtest.xls";//指定excel文件$objPHPExcel = $objReader->load($filename); 
//$filename可以是上传的文件,或者是指定的文件$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); // 取得总行数 $highestColumn = $sheet->getHighestColumn(); 
// 取得总列数$k = 0;//循环读取excel文件,读取一条,插入一条
//j表示从哪一行开始读取
//$a表示列号for($j=2;$j<=$highestRow;$j++)
{    $a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//获取A列的值
    $b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//获取B列的值
    $c = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//获取C列的值
    $sql = "INSERT INTO table VALUES(".$a.",".$b.",".$c.")";    print_r($sql);exit;
}?>
Copy after login

3、确认打印出来的sql语句,如果正确即可删除exit进行循环插入

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程 

Related labels:
source:cnblogs.com
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template