php excelreader读取excel文件实例教程

发布时间:2020-08-26编辑:脚本学堂
本文介绍了php中使用excelreader读取excel文件的方法,php-excel-reader可以读取excel文件,这是一个不错的phpexcel类库。

php使用excelreader读取excel文件

php开发中肯定会遇到将excel文件内容导入到数据库的需要,php-excel-reader是一个读取excel的类,可以很轻松的使用它读取excel文件非常方便。
php-excel-reader下载地址: http://www.jb200.com/down/135.html

本文使用的是php-excel-reader-2.21版本。

php实例:
excel如下图:
php使用excelreader读取excel文件1

例子,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文件2

这个类的小问题:
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");才行。

有希望了解phpexcel类库的朋友,可以阅读下phpexcel中文帮助手册中的内容,具体的实例大家可以phpexcel快速开发指南中的相关例子。

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
)
)

使用php-excel-reader读取excel文件很简单,希望大家都可以掌握。