网站地图    收藏   

主页 > php专栏 > php应用 >

PHP Excel类读取excel文件并且导入数据库 - php高级应

来源:自学PHP网    时间:2014-11-27 22:16 作者: 阅读:

[导读] 在一般情况下如果我们要把excel数据库中的数据导入到mysql数据库我们没什么好办法实现了,但有了PHP Excel失控这后一切都变得简单了.本代码是在thinkphp中开始使用的.1.引入类,代码如下:...

PHP Excel类读取excel文件并且导入数据库

在一般情况下如果我们要把excel数据库中的数据导入到mysql数据库我们没什么好办法实现了,但有了PHP Excel失控这后一切都变得简单了.

本代码是在thinkphp中开始使用的.

1.引入类,代码如下:

  1. Vendor('PHPExcel.PHPExcel');//引入扩展类.就是/ 
  2.         Vendor('PHPExcel.PHPExcel.IOFactory'); 
  3.         Vendor('PHPExcel.PHPExcel.Reader.Excel5'); 
  4.  
  5. $excel_file= ROOT_PATH."/public/Uploads/".$publicity_bankdata_mod->where("id=".$data['id'])->getField('excel_file'); 
  6.         //dump($excel_file);exit; 
  7. $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format 
  8. $objPHPExcel = $objReader->load($excel_file);//$uploadfile 
  9. $sheet = $objPHPExcel->getSheet(0); 
  10. $highestRow = $sheet->getHighestRow(); // 取得总行数 
  11. $highestColumn = $sheet->getHighestColumn(); // 取得总列数 
  12. $arr_result = array(); 
  13. $strs=array(); 
  14. $strs_datas=array(); 
  15. $succ_result=0; 
  16. $error_result=0; 

上面看上去有点乱,下面我来写一个完整的类,代码如下:

  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连接数据库 
  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绑定参数操作 
  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(); // 取得总行数 
  27. $highestColumn = $sheet->getHighestColumn(); // 取得总列数 
  28.  
  29. for($j=1;$j<=10;$j++) 
  30.  
  31. $student_no = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//第一列学号 
  32. $name = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//第二列姓名 
  33. $gid = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//第三列gid 
  34. //将获取的excel内容插入到数据库 
  35. //开源代码phpfensi.com 
  36. $stmt->execute(); 
  37. ?>  

php-excel-reader操作excel中的两个重要的方法:

1.dump(),它可以将excel内容以html格式输出:echo $data->dump(true,true);

2.将excel数据存入数组中,使用$data->sheets,打印下如下:

  1. Array  
  2. (  
  3. [0] => Array  
  4. (  
  5. [maxrow] => 0  
  6. [maxcol] => 0  
  7. [numRows] => 5  
  8. [numCols] => 4  
  9. [cells] => Array  
  10. (  
  11. [1] => Array  
  12. (  
  13. [1] => 编号  
  14. [2] => 姓名  
  15. [3] => 年龄  
  16. [4] => 学号  
  17. )  
  18. [2] => Array  
  19. (  
  20. [1] => 1  
  21. [2] => 小红  
  22. [3] => 22  
  23. [4] => a1000  
  24. )  
  25. [3] => Array  
  26. (  
  27. [1] => 2  
  28. [2] => 小王  
  29. [3] => 33  
  30. [4] => a1001  
  31. )  
  32. [4] => Array  
  33. (  
  34. [1] => 3  
  35. [2] => 小黑  
  36. [3] => 44  
  37. [4] => a1002  
  38. )  
  39. [5] => Array  
  40. (  
  41. [2] => by  
  42. [3] => www.phpfensi.com  
  43. )  
  44. )  
  45. [cellsInfo] => Array  
  46. (  
  47. [1] => Array  
  48. (  
  49. [1] => Array  
  50. (  
  51. [xfIndex] => 15  
  52. )  
  53. [2] => Array  
  54. (  
  55. [xfIndex] => 15  
  56. )  
  57. [3] => Array  
  58. (  
  59. [xfIndex] => 15  
  60. )  
  61. [4] => Array  
  62. (  
  63. [xfIndex] => 15  
  64. )  
  65. )  
  66. [2] => Array  
  67. (  
  68. [1] => Array  
  69. (  
  70. [string] => 1  
  71. [raw] => 1  
  72. [rectype] => unknown  
  73. [format] => %s  
  74. [formatIndex] => 0  
  75. [fontIndex] => 0  
  76. [formatColor] =>  
  77. [xfIndex] => 15  
  78. )  
  79. [2] => Array  
  80. (  
  81. [xfIndex] => 15  
  82. )  
  83. [3] => Array  
  84. (  
  85. [string] => 22  
  86. [raw] => 22  
  87. [rectype] => unknown  
  88. [format] => %s  
  89. [formatIndex] => 0  
  90. [fontIndex] => 0  
  91. [formatColor] =>  
  92. [xfIndex] => 15  
  93. )  
  94. [4] => Array  
  95. (  
  96. [xfIndex] => 15  
  97. )  
  98. )  
  99. [3] => Array  
  100. (  
  101. [1] => Array  
  102. (  
  103. [string] => 2  
  104. [raw] => 2  
  105. [rectype] => unknown  
  106. [format] => %s  
  107. [formatIndex] => 0  
  108. [fontIndex] => 6  
  109. [formatColor] =>  
  110. [xfIndex] => 23  
  111. )  
  112. [2] => Array  
  113. (  
  114. [xfIndex] => 23  
  115. )  
  116. [3] => Array  
  117. (  
  118. [string] => 33  
  119. [raw] => 33  
  120. [rectype] => unknown  
  121. [format] => %s  
  122. [formatIndex] => 0  
  123. [fontIndex] => 6  
  124. [formatColor] =>  
  125. [xfIndex] => 23  
  126. )  
  127. [4] => Array  
  128. (  
  129. [xfIndex] => 23  
  130. )  
  131. )  
  132. [4] => Array  
  133. (  
  134. [1] => Array  
  135. (  
  136. [string] => 3  
  137. [raw] => 3  
  138. [rectype] => unknown  
  139. [format] => %s  
  140. [formatIndex] => 0  
  141. [fontIndex] => 0  
  142. [formatColor] =>  
  143. [xfIndex] => 15  
  144. )  
  145. [2] => Array  
  146. (  
  147. [xfIndex] => 15  
  148. )  
  149. [3] => Array  
  150. (  
  151. [string] => 44  
  152. [raw] => 44  
  153. [rectype] => unknown  
  154. [format] => %s  
  155. [formatIndex] => 0  
  156. [fontIndex] => 0  
  157. [formatColor] =>  
  158. [xfIndex] => 15  
  159. )  
  160. [4] => Array  
  161. (  
  162. [xfIndex] => 15  
  163. )  
  164. )  
  165. [5] => Array  
  166. (  
  167. [2] => Array  
  168. (  
  169. [xfIndex] => 15  
  170. )  
  171. [3] => Array  
  172. (  
  173. [xfIndex] => 24  
  174. [hyperlink] => Array  
  175. (  
  176. [flags] => 23  
  177. [desc] => www.phpfensi.com 
  178.  
  179. [link] => http://www.phpfensi.com  
  180. )  
  181. )  
  182. )  
  183. )  
  184. )  
  185. [1] => Array  
  186. (  
  187. [maxrow] => 0  
  188. [maxcol] => 0  
  189. [numRows] => 0  
  190. [numCols] => 0  
  191. )  
  192. [2] => Array  
  193. (  
  194. [maxrow] => 0  
  195. [maxcol] => 0  
  196. [numRows] => 0  
  197. [numCols] => 0  
  198. )  
  199. )

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论