国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHPExcel讀取excel并導(dǎo)入數(shù)據(jù)庫

2024-05-04 21:49:18
字體:
供稿:網(wǎng)友

PHPExcel是一款php對于excel數(shù)據(jù)表讀寫的一個非常棒的插件了,下面我來給大家介紹利用PHPExcel讀取excel并導(dǎo)入mysql數(shù)據(jù)庫方法.

例1,代碼示例,代碼如下:

  1. require_once 'phpexcel/Classes/PHPExcel.php'
  2. require_once 'phpexcel/Classes/PHPExcel/IOFactory.php'
  3. require_once 'phpexcel/Classes/PHPExcel/Reader/Excel5.php'
  4. $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format 
  5. $objPHPExcel = $objReader->load($filename); //$filename可以是上傳的文件,或者是指定的文件 
  6. $sheet = $objPHPExcel->getSheet(0); 
  7. $highestRow = $sheet->getHighestRow(); // 取得總行數(shù) 
  8. $highestColumn = $sheet->getHighestColumn(); // 取得總列數(shù) 
  9. $k = 0; 
  10. //循環(huán)讀取excel文件,讀取一條,插入一條 
  11. for($j=2;$j<=$highestRow;$j++) 
  12. $a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//獲取A列的值 
  13. $b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//獲取B列的值 
  14. $sql = "INSERT INTO table VALUES(".$a.",".$b.")"
  15. mysql_query($sql); 

例2,代碼如下:

  1. <?php 
  2. set_time_limit(20000); 
  3. ini_set('memory_limit','-1'); 
  4. require_once './PHPExcel.php'
  5. require_once './PHPExcel/IOFactory.php'
  6. require_once './PHPExcel/Reader/Excel5.php'
  7.  
  8. //使用pdo連接數(shù)據(jù)庫 
  9. $dsn = "mysql:host=localhost;dbname=alumni;"
  10. $user = "root"
  11. $password = ""
  12. try{ 
  13.  $dbh = new PDO($dsn,$user,$password); 
  14.  $dbh->query('set names utf8;');  
  15. }catch(PDOException $e){ 
  16.  echo "連接失敗".$e->getMessage(); 
  17. //pdo綁定參數(shù)操作 
  18. $stmt = $dbh->prepare("insert into alumni(gid,student_no,name) values (:gid,:student_no,:name) "); 
  19. $stmt->bindParam(":gid"$gid,PDO::PARAM_STR); 
  20. $stmt->bindParam(":student_no"$student_no,PDO::PARAM_STR); 
  21. $stmt->bindParam(":name"$name,PDO::PARAM_STR); 
  22.  
  23. $objReader = new PHPExcel_Reader_Excel5(); //use excel2007 
  24. $objPHPExcel = $objReader->load('bks.xls'); //指定的文件 
  25. $sheet = $objPHPExcel->getSheet(0); 
  26. $highestRow = $sheet->getHighestRow(); // 取得總行數(shù) 
  27. $highestColumn = $sheet->getHighestColumn(); // 取得總列數(shù) 
  28. //開源代碼Vevb.com 
  29. for($j=1;$j<=10;$j++) 
  30.  
  31. $student_no = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//第一列學(xué)號 
  32. $name = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//第二列姓名 
  33. $gid = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//第三列g(shù)id 
  34. //將獲取的excel內(nèi)容插入到數(shù)據(jù)庫 
  35. $stmt->execute(); 
  36. ?> 

例3,新建數(shù)據(jù)庫表如下:

  1. -- 數(shù)據(jù)庫: `alumni` 
  2. -- 表的結(jié)構(gòu) `alumni` 
  3. CREATE TABLE IF NOT EXISTS `alumni` ( 
  4. `id` bigint(20) NOT NULL AUTO_INCREMENT, 
  5. `gid` varchar(20) DEFAULT NULL COMMENT '檔案編號'
  6. `student_no` varchar(20) DEFAULT NULL COMMENT '學(xué)號'
  7. `namevarchar(32) DEFAULT NULL
  8. PRIMARY KEY (`id`), 
  9. KEY `gid` (`gid`), 
  10. KEY `name` (`name`) 
  11. ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

php程序,代碼如下:

  1. <?php  
  2. header("Content-Type:text/html;charset=utf-8");  
  3. require_once 'excel_reader2.php';  
  4. set_time_limit(20000);  
  5. ini_set("memory_limit","2000M");  
  6. //使用pdo連接數(shù)據(jù)庫  
  7. $dsn = "mysql:host=localhost;dbname=alumni;";  
  8. $user = "root";  
  9. $password = "";  
  10. try{  
  11. $dbh = new PDO($dsn,$user,$password);  
  12. $dbh->query('set names utf8;');  
  13. }catch(PDOException $e){  
  14. echo "連接失敗".$e->getMessage();  
  15. }  
  16. //pdo綁定參數(shù)操作  
  17. $stmt = $dbh->prepare("insert into alumni(gid,student_no,name) values (:gid,:student_no,:name) ");  
  18. $stmt->bindParam(":gid"$gid,PDO::PARAM_STR);  
  19. $stmt->bindParam(":student_no"$student_no,PDO::PARAM_STR);  
  20. $stmt->bindParam(":name"$name,PDO::PARAM_STR);  
  21. //使用php-excel-reader讀取excel內(nèi)容  
  22. $data = new Spreadsheet_Excel_Reader();  
  23. $data->setOutputEncoding('UTF-8');  
  24. $data->read("stu.xls");  
  25. for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {  
  26. for ($j = 1; $j <= 3; $j++) {  
  27. $student_no = $data->sheets[0]['cells'][$i][1];  
  28. $name = $data->sheets[0]['cells'][$i][2];  
  29. $gid = $data->sheets[0]['cells'][$i][3];  
  30. }  
  31. //將獲取的excel內(nèi)容插入到數(shù)據(jù)庫  
  32. $stmt->execute();  
  33. }  
  34. echo "執(zhí)行成功";  
  35. echo "最后插入的ID:".$dbh->lastInsertId();  
  36. ?>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 鲁甸县| 安福县| 崇文区| 玉田县| 兴山县| 长海县| 泉州市| 都兰县| 凉城县| 淮南市| 滨州市| 曲麻莱县| 高碑店市| 龙陵县| 绵竹市| 镇雄县| 绥芬河市| 依安县| 新营市| 中江县| 玉树县| 黎川县| 平山县| 青海省| 凤城市| 奉新县| 湘潭县| 平凉市| 博客| 阜阳市| 仪陇县| 贵州省| 泊头市| 东宁县| 广河县| 武汉市| 襄垣县| 中超| 康马县| 寿阳县| 临洮县|