PHPExcel读取excel小例子
PHP #phpexcel #excel2014-04-09 22:24
直接上代码。
<?php
set_time_limit(20000);
ini_set('memory_limit','-1');
require_once './PHPExcel.php';
require_once './PHPExcel/IOFactory.php';
require_once './PHPExcel/Reader/Excel5.php';
//使用pdo连接数据库
$dsn = "mysql:host=localhost;dbname=yige-org;";
$user = "root";
$password = "";
try{
$dbh = new PDO($dsn,$user,$password);
$dbh->query('set names utf8;');
}catch(PDOException $e){
echo "连接失败".$e->getMessage();
}
//pdo绑定参数操作 http://yige.org/
$stmt = $dbh->prepare("insert into yige-org(gid,student_no,name) values (:gid,:student_no,:name) ");
$stmt->bindParam(":gid", $gid,PDO::PARAM_STR);
$stmt->bindParam(":student_no", $student_no,PDO::PARAM_STR);
$stmt->bindParam(":name", $name,PDO::PARAM_STR);
$objReader = new PHPExcel_Reader_Excel5(); //use excel2007
$objPHPExcel = $objReader->load('bks.xls'); //指定的文件
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
for($j=1;$j<=10;$j++)
{
$student_no = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//第一列学号
$name = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//第二列姓名
$gid = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//第三列gid
}
//将获取的excel内容插入到数据库
$stmt->execute();相关文章
- PHP中set_time_limit()函数用法 2014/04/09
- PHP防止漏洞技巧 2014/04/09
- PHP代码简单加密和解密的实现 2014/04/09
- php用GD2生成文字图片的方法 2014/04/09
- PHP面试题常见算法题 2014/04/09
- Linux下PHP Error Class 'SoapClient' not found解决方法 2014/04/09
- php输出缓冲技术应用实践 2014/04/09
- php结合js实现多文件上传 2014/04/09
- PHP最全防止sql注入的方法 2014/04/08
- php开发技巧 2014/04/08