phpexcel读取excel并导入mysql数据库_PHPExcel读取excel并导入mysql数据库代码

更新时间:2018-01-12    来源:excel    手机版     字体:

【www.bbyears.com--excel】

PHPExcel是相当强大的 MS Office Excel 文档生成类库,当需要输出比较复杂格式数据的时候,PHPExcel 是个不错的选择。不过其使用方法相对来说也就有些繁琐

 代码如下

    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=alumni;";
    $user = "root";
    $password = "";
    try{
    $dbh = new PDO($dsn,$user,$password);
    $dbh->query("set names utf8;");
    }catch(PDOException $e){
    echo "连接失败".$e->getMessage();
    }
    //pdo绑定参数操作
    $stmt = $dbh->prepare("insert into alumni(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();
?>

本文来源:http://www.bbyears.com/bangongshuma/38467.html

热门标签

更多>>

本类排行