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


Posted in PHP onDecember 06, 2012

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

php-excel-reader下载地址: https://3water.com/codes/67223.html

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

我使用的excel如下图:

php读取EXCEL文件 php excelreader读取excel文件php代码如下:

<?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文件 php excelreader读取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文件就是这么简单
PHP 相关文章推荐
删除无限级目录与文件代码共享
Jul 12 PHP
php 过滤器实现代码
Aug 09 PHP
php中通过Ajax如何实现异步文件上传的代码实例
May 07 PHP
php写的简易聊天室代码
Jun 04 PHP
PHP读取网页文件内容的实现代码(fopen,curl等)
Jun 23 PHP
php设计模式 Adapter(适配器模式)
Jun 26 PHP
php实现把数组按指定的个数分隔
Feb 17 PHP
一个经典实用的PHP图像处理类分享
Nov 18 PHP
Thinkphp框架 表单自动验证登录注册 ajax自动验证登录注册
Dec 27 PHP
PHP5.0 TIDY_PARSE_FILE缓冲区溢出漏洞的解决方案
Oct 14 PHP
解决laravel groupBy 对查询结果进行分组出现的问题
Oct 09 PHP
PHP 计算两个时间段之间交集的天数示例
Oct 24 PHP
php excel reader读取excel内容存入数据库实现代码
Dec 06 #PHP
PHPMailer使用教程(PHPMailer发送邮件实例分析)
Dec 06 #PHP
php单例模式实现(对象只被创建一次)
Dec 05 #PHP
利用php实现禁用IE和火狐的缓存问题
Dec 03 #PHP
php自动给文章加关键词链接的函数代码
Nov 29 #PHP
PHP文章按日期(月日)SQL归档语句
Nov 29 #PHP
PHP以及MYSQL日期比较方法
Nov 29 #PHP
You might like
《雄兵连》《烈阳天道》真的来了
2020/07/13 国漫
php中将汉字转换成拼音的函数代码
2012/09/08 PHP
php缓冲 output_buffering和ob_start使用介绍
2014/01/30 PHP
php实现通过soap调用.Net的WebService asmx文件
2017/02/27 PHP
yii2学习教程之5种内置行为类详解
2017/08/03 PHP
JavaScript 使用技巧精萃(.net html
2009/04/25 Javascript
js使用ajax读博客rss示例
2014/05/06 Javascript
jQuery实现的多级下拉菜单效果代码
2015/08/24 Javascript
举例说明JavaScript中的实例对象与原型对象
2016/03/11 Javascript
JQuery实现简单的服务器轮询效果实例
2016/03/31 Javascript
AngularJs表单验证实例详解
2016/05/30 Javascript
利用yarn实现一个webpack+react种子
2016/10/25 Javascript
使用classList来实现两个按钮样式的切换方法
2018/01/24 Javascript
vue中如何去掉空格的方法实现
2018/11/09 Javascript
vue使用echarts实现水平柱形图实例
2020/09/09 Javascript
使用AutoJs实现微信抢红包的代码
2020/12/31 Javascript
[01:03:51]2018DOTA2亚洲邀请赛 4.7 淘汰赛 VP vs LGD 第三场
2018/04/09 DOTA
[47:46]完美世界DOTA2联赛 Magma vs GXR 第三场 11.07
2020/11/10 DOTA
在Python的Django框架中编写编译函数
2015/07/20 Python
Flask的图形化管理界面搭建框架Flask-Admin的使用教程
2016/06/13 Python
Django后端接收嵌套Json数据及解析详解
2019/07/17 Python
Python使用多进程运行含有任意个参数的函数
2020/05/02 Python
HTML5标签小集
2011/08/02 HTML / CSS
iframe与window.onload如何使用详解
2020/05/07 HTML / CSS
国外平面设计素材网站:The Hungry JPEG
2017/03/28 全球购物
Willer台湾:日本高速巴士/夜行巴士预约
2017/07/09 全球购物
体育教师自荐信范文
2013/12/16 职场文书
小学生学习感言
2014/03/10 职场文书
大学专科自荐信
2014/06/17 职场文书
活动总结新闻稿
2014/08/30 职场文书
社团个人总结范文
2015/03/05 职场文书
2015年重阳节慰问信
2015/03/23 职场文书
活动总结模板大全
2015/05/11 职场文书
毕业论文答辩开场白和答辩技巧
2015/05/27 职场文书
MySQL基础(一)
2021/04/05 MySQL
Python内置数据结构列表与元组示例详解
2021/08/04 Python