Home > php教程 > php手册 > body text

使用phpexcel类实现excel导入mysql数据库功能(实例代码),phpexcelmysql

WBOY
Release: 2016-06-13 08:40:22
Original
890 people have browsed it

使用phpexcel类实现excel导入mysql数据库功能(实例代码),phpexcelmysql

下载phpexcel文件,地址:phpexcel.codeplex.com/

代码示例

require_once 'phpexcel/Classes/PHPExcel.php';
require_once 'phpexcel/Classes/PHPExcel/IOFactory.php';
require_once 'phpexcel/Classes/PHPExcel/Reader/Excel5.php';
$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format 
$objPHPExcel = $objReader->load($filename); //$filename可以是上传的文件,或者是指定的文件
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); // 取得总行数 
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
$k = 0; 

//循环读取excel文件,读取一条,插入一条
for($j=2;$j<=$highestRow;$j++)
{

$a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//获取A列的值
$b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//获取B列的值
$sql = "INSERT INTO table VALUES(".$a.",".$b.")";
mysql_query($sql);

}

Copy after login

以上这篇使用phpexcel类实现excel导入mysql数据库功能(实例代码)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持帮客之家。

source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!