php读取EXCEL文件 php excelreader读取excel文件

所属分类: 网络编程 / PHP编程 阅读数: 1608
收藏 0 赞 0 分享

php开发中肯定会遇到将excel文件内容导入到数据库的需要,php-excel-reader是一个读取excel的类,可以很轻松的使用它读取excel文件非常方便。

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

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

我使用的excel如下图:

php-excel-readerphp代码如下:

复制代码 代码如下:

<?php
/*by www.phpddt.com*/
header("Content-Type:text/html;charset=utf-8");
require_once 'excel_reader2.php';
//创建对象
$data = new Spreadsheet_Excel_Reader();
//设置文本输出编码
$data->setOutputEncoding('UTF-8');
//读取Excel文件
$data->read("example.xls");
//$data->sheets[0]['numRows']为Excel行数
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
//$data->sheets[0]['numCols']为Excel列数
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
//显示每个单元格内容
echo $data->sheets[0]['cells'][$i][$j].' ';
}
echo '<br>';
}
?>

读取结果截图如下

php-excel-reader读取excel文件再来说说这个类的小问题:

(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,打印下如下:

复制代码 代码如下:

Array
(
[0] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 5
[numCols] => 4
[cells] => Array
(
[1] => Array
(
[1] => 编号
[2] => 姓名
[3] => 年龄
[4] => 学号
)
[2] => Array
(
[1] => 1
[2] => 小红
[3] => 22
[4] => a1000
)
[3] => Array
(
[1] => 2
[2] => 小王
[3] => 33
[4] => a1001
)
[4] => Array
(
[1] => 3
[2] => 小黑
[3] => 44
[4] => a1002
)
[5] => Array
(
[2] => by
[3] => www.phpddt.com
)
)
[cellsInfo] => Array
(
[1] => Array
(
[1] => Array
(
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[2] => Array
(
[1] => Array
(
[string] => 1
[raw] => 1
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[string] => 22
[raw] => 22
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[3] => Array
(
[1] => Array
(
[string] => 2
[raw] => 2
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 6
[formatColor] =>
[xfIndex] => 23
)
[2] => Array
(
[xfIndex] => 23
)
[3] => Array
(
[string] => 33
[raw] => 33
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 6
[formatColor] =>
[xfIndex] => 23
)
[4] => Array
(
[xfIndex] => 23
)
)
[4] => Array
(
[1] => Array
(
[string] => 3
[raw] => 3
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[string] => 44
[raw] => 44
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[5] => Array
(
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[xfIndex] => 24
[hyperlink] => Array
(
[flags] => 23
[desc] => www.phpddt.com
[link] => http://www.phpddt.co
)
)
)
)
)
[1] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 0
[numCols] => 0
)
[2] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 0
[numCols] => 0
)
)

这样你应该知道怎么取excel中的数据了,好了,使用php-excel-reader读取excel文件就是这么简单

更多精彩内容其他人还在看

Python中使用django form表单验证的方法

这篇文章主要介绍了Python中使用django form表单验证的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

php文件管理基本功能简单操作

这篇文章主要为大家详细介绍了php文件管理基本功能简单操作的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

php常用数组函数实例小结

这篇文章主要介绍了php常用数组函数,结合实例形式总结分析了php常用数组函数array_merge、array_slice及array_map的功能与使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

详解ThinkPHP3.2.3验证码显示、刷新、校验

本篇文章主要介绍了ThinkPHP3.2.3验证码显示、刷新、校验 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

php常用正则函数实例小结

这篇文章主要介绍了php常用正则函数,结合实例形式总结分析了php正则表达式常用函数,包括preg_replace、preg_match及preg_match_all函数的功能、使用方法与相关注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

php常用字符函数实例小结

这篇文章主要介绍了php常用字符函数,结合实例形式总结分析了php常用字符函数substr、preg_match、strpos、dirname及str_split功能、用法与相关注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP实现的XML操作类【XML Library】

这篇文章主要介绍了PHP实现的XML操作类,涉及php针对数组、xml的转换、序列化、反序列化等相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

详解thinkphp实现excel数据的导入导出(附完整案例)

本篇文章主要介绍了thinkphp实现excel数据的导入导出,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

PHP实现截取中文字符串不出现?号的解决方法

这篇文章主要介绍了PHP实现截取中文字符串不出现?号的解决方法,涉及php字符串遍历及编码转换等相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

微信公众号模板消息群发php代码示例

这篇文章主要为大家详细介绍了微信公众号模板消息群发php代码示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多