发布于 2015-01-22 04:55:57 | 197 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

PHP-ExcelReader Excel操作类库

PHP-ExcelReader是一个读取Excel xsl文件内容的一个PHP类.它需要OLE包支持,但不需要运行在Windows平台上.您能从这里获取到OLE包.


本文为大家讲解的是php结合PHP-ExcelReader读取excel文件示例代码,感兴趣的同学参考下。

PHP-ExcelReader是一个读取Excel xsl文件内容的一个PHP类.它需要OLE包支持,但不需要运行在Windows平台上.您能从这里获取到OLE包.

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

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

我使用的excel如下图:

php代码如下:


<?php
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>';
}
?>


读取结果截图如下

 

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

(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文件就是这么简单

 



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务