当前位置: 网学 > 编程文档 > PHP > 正文

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

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/06/18

【编者按】网学网PHP频道为大家收集整理了“php读取EXCEL文件 php excelreader读取excel文件“提供大家参考,希望对大家有所帮助!

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

php-excel-reader下载地址: /uploadfile/201306/18/B615349187.png">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-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
(
=> Array
(
=> 编号
=> 姓名
=> 年龄
=> 学号
)
=> Array
(
=> 1
=> 小红
=> 22
=> a1000
)
=> Array
(
=> 2
=> 小王
=> 33
=> a1001
)
=> Array
(
=> 3
=> 小黑
=> 44
=> a1002
)
=> Array
(
=> by
=> www.phpddt.com
)
)
[cellsInfo] => Array
(
=> Array
(
=> Array
(
[xfIndex] => 15
)
=> Array
(
[xfIndex] => 15
)
=> Array
(
[xfIndex] => 15
)
=> Array
(
[xfIndex] => 15
)
)
=> Array
(
=> Array
(
[string] => 1
[raw] => 1
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
=> Array
(
[xfIndex] => 15
)
=> Array
(
[string] => 22
[raw] => 22
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
=> Array
(
[xfIndex] => 15
)
)
=> Array
(
=> Array
(
[string] => 2
[raw] => 2
[rectype] => unknown
[format] =&

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号