找回密码
 注册账号

QQ登录

只需一步,快速开始

《泰拉瑞亚下载-1.4.2.3版》 泰拉瑞亚服务器 - MOD模组下载

入驻泰拉战网 新手指引 - 升级 - 师徒

泰拉瑞亚合成表 泰拉卡牌 - 泰拉江湖 - 泰拉刺客

联系泰拉开发组👈进入 积分市场 - 房产交易 - 水晶获取

查看: 2875|回复: 0

[原生PHP] php读取EXCEL文件 php excelreader读取excel文件

[复制链接]

431

主题

77

回帖

8

广播

论坛版主

积分
214
泰拉
0
水晶
10
铜钥匙
0
银钥匙
0
金钥匙
0

【江湖新秀】【宝剑回鞘】【泰拉达人】【奥运选手】

发表于 2020-3-24 09:47:04 | 显示全部楼层 |阅读模式
php开发中肯定会遇到将excel文件内容导入到数据库的需要,php-excel-reader是一个读取excel的类,可以很轻松的使用它读取excel文件非常方便。

php-excel-reader下载地址: //www.jb51.net/codes/67223.html

我下载的是php-excel-reader-2.21版本,使用的时候还遇到几个小问题,后面再细说,先奉上php实例:

我使用的excel如下图:

  1. <?php
  2. /*by www.phpddt.com*/
  3. header("Content-Type:text/html;charset=utf-8");
  4. require_once 'excel_reader2.php';
  5. //创建对象
  6. $data = new Spreadsheet_Excel_Reader();
  7. //设置文本输出编码
  8. $data->setOutputEncoding('UTF-8');
  9. //读取Excel文件
  10. $data->read("example.xls");
  11. //$data->sheets[0]['numRows']为Excel行数
  12. for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
  13. //$data->sheets[0]['numCols']为Excel列数
  14. for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
  15. //显示每个单元格内容
  16. echo $data->sheets[0]['cells'][$i][$j].' ';
  17. }
  18. echo '<br>';
  19. }
  20. ?>
复制代码


再来说说这个类的小问题:

(1)出现Deprecated: Function split() is deprecated in 。。。错误

解决:将excel_reader2.php源码中split改为explode,详情点击php中explode与split的区别介绍

(2)出现Deprecated: Assigning the return value of new by reference is deprecated in错误

解决:将excel_reader2.php源码中$this->_ole =& new OLERead()中 &去掉,因为php5.3中废除了=& 符号直接用=引用

(3)乱码问题解决:

构造函数是function Spreadsheet_Excel_Reader($file='',$store_extended_info=true,$outputEncoding=''),它默认的编码是utf-8,如果不指定,可能会出现乱码问题,可通过$data->setOutputEncoding('GBK');指定,还有如果你使用dump()函数,dump()函数将excel内容一html格式输出,使用htmlentities将字符转化为html的,它默认使用ISO8559-1编码的,所以你要将 excel_reader2.php源码中 htmlentities($val)函数改为htmlentities($val,ENT_COMPAT,"GB2312");才行。

最后来说说,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.phpddt.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.phpddt.com
  178. [link] => http://www.phpddt.co
  179. )
  180. )
  181. )
  182. )
  183. )
  184. [1] => Array
  185. (
  186. [maxrow] => 0
  187. [maxcol] => 0
  188. [numRows] => 0
  189. [numCols] => 0
  190. )
  191. [2] => Array
  192. (
  193. [maxrow] => 0
  194. [maxcol] => 0
  195. [numRows] => 0
  196. [numCols] => 0
  197. )
  198. )
复制代码


[发帖际遇]: mickeyort 乐于助人,奖励 10 泰拉. 幸运榜 / 衰神榜
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

QQ|友链申请|Archiver|手机版|小黑屋|游芯沙盒 ( 陕ICP备11006283号-1 )

GMT+8, 2024-12-4 01:25 , Processed in 0.101906 second(s), 37 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表