phpexcel读取excel的xls xlsx csv格式
我之前写过一篇PHP读取csv文件的内容
上代码index.php
<?php /**
*
* @author XC
*
*/
class Excel
{
public $currentSheet;
public $filePath;
public $fileType;
public $sheetIndex=;
public $allColumn;
public $allRow;
public function initialized($filePath) {
if (file_exists($filePath)) {
$this->filePath=$filePath;
}else{
return array();
}
//以硬盘方式缓存
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_discISAM;
$cacheSettings = array();
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$file_ext=strtolower(pathinfo($this->filePath, PATHINFO_EXTENSION));
switch ($file_ext) {
case 'csv':
$this->fileType='csv';
break;
case 'xlsx':
$this->fileType='excel';
break;
case 'xls':
$this->fileType='excel';
break;
default:
$this->fileType='';
break;
} if ($this->fileType=='csv') {
$PHPReader = new PHPExcel_Reader_CSV(); //默认字符集
$PHPReader->setInputEncoding('GBK'); //默认分隔符
$PHPReader->setDelimiter(','); if(!$PHPReader->canRead($this->filePath)){
return array();
}
}elseif ($this->fileType=='excel'){
$PHPReader = new PHPExcel_Reader_Excel2007(); if(!$PHPReader->canRead($this->filePath)){
$PHPReader = new PHPExcel_Reader_Excel5(); if(!$PHPReader->canRead($this->filePath)){
return array();
}
}
}else{
return array();
} $PHPReader->setReadDataOnly(true);
$PHPExcel = $PHPReader->load($this->filePath);
$this->currentSheet = $PHPExcel->getSheet((int)$this->sheetIndex);
//$this->currentSheet = $PHPExcel->getActiveSheet();
$this->allColumn=$this->currentSheet->getHighestColumn();
$this->allRow=$this->currentSheet->getHighestRow();
} public function fetch($beginRow=NULL, $endRow=NULL){
$currentSheet=$this->currentSheet;
$allColumn=$this->allColumn;$allRow=$this->allRow;
$dataSrc=$data=array(); //获取列标题
for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - , )->getValue();//ord()將字符转为十进制数
$dataSrc[ord($currentColumn) - ]=strtolower(trim($val));}
//echo implode("\t", $dataSrc);
$beginRow=$beginRow ? $beginRow : ;
$endRow=$endRow ? $endRow : $allRow;
for($currentRow = $beginRow ;$currentRow <= $endRow ;$currentRow++){
//从第A列开始输出$dataRow=array();
for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - ,$currentRow)->getValue();//ord()將字符转为十进制数
$dataRow[$dataSrc[ord($currentColumn) - ]]=$val;
//单元级数据处理 ... 格式化日期等
} //行级数据处理 ... if($dataRow){
$data[]=$dataRow;}
} //echo '<pre>', print_r($data), '</pre>'; //echo "\n";
return $data;
}
} require_once 'Classes/PHPExcel.php';
$import=new Excel();
$import->initialized(dirname(__FILE__) . '/test.xlsx');
header("Content-type: text/html; charset=utf-8");
echo '<pre>', print_r($import->fetch()), '</pre>'; ?>
其中的$import->initialized(dirname(__FILE__) . '/test.xlsx'); test.xlsx修改为自己的execl路径
require_once 'Classes/PHPExcel.php';修改为你自己PHPExcel.php所在的路径
PHPExcel.php可从官网下载 http://phpexcel.codeplex.com/
phpexcel读取excel的xls xlsx csv格式的更多相关文章
- java读取excel文件(.xls,xlsx,csv)
前提,maven工程通过poi读写excel文件,需要在pom.xml中配置依赖关系: 在<dependencies>中添加如下代码 <dependency> <grou ...
- PHPExcel读取excel文件示例
PHPExcel读取excel文件示例PHPExcel最新版官方下载网址:http://phpexcel.codeplex.com/PHPExcel是一个非常方便生成Excel格式文件的类,官方下载包 ...
- 利用PHPExcel读取Excel的数据和导出数据到Excel
PHPExcel是一个PHP类库,用来帮助我们简单.高效实现从Excel读取Excel的数据和导出数据到Excel.也是我们日常开发中,经常会遇到的使用场景.比如有个客户信息表,要批量导出发给同事,我 ...
- (实用篇)PHPExcel读取Excel文件的实现代码
用PHPExcel读取Excel 2007 或者Excel2003文件,需要的朋友,可以参考下. 涉及知识点: php对excel文件进行循环读取 php对字符进行ascii编码转化,将字符转为十进 ...
- 数组转xls格式的excel文件&数据转csv格式的excle
/** * 数组转xls格式的excel文件 * @param array $data 需要生成excel文件的数组 * @param string $filename 生成的excel文件名 * 示 ...
- Delphi - cxGrid内容xlsx、xls、csv格式导出
.xls格式导出,uses中添加cxGridExportLink 代码如下: function SaveToExcel(gridMain: TcxGrid; FileName: string): st ...
- django 操作 下载 excel xls xlsx csv
网站开发离不开数据的导入导出,本文将介绍一下django如何操作excel 先安装 django-excel pip install django-excel 配置一下url url(r'^downl ...
- Yii2使用PHPExcel读取excel
个人使用过程中保存一些使用PHPExcel的经验,以便后来翻阅:与PHP的Yii框架结合,可以轻松使用.而且根本不用网上所说的修改Yii的自动加载文件等方法.具体使用方法:下载phpoffice ht ...
- PHPExcel读取Excel文件的实现代码
<?php require_once 'PHPExcel.php'; /**对excel里的日期进行格式转化*/ function GetData($val){ $jd = GregorianT ...
随机推荐
- CentOS7 学习笔记
1.首先centos7 采用了systemd管理系统服务的启动 systemd结合了以前红帽子的service 与chkconfig systemctl [command] [unit] comm ...
- 快速掌握、学习HTML的方法
链接 超级链接用<a>标签表示,href属性指定了链接到的地址.<a>标签可以包含文本,也可以包含图片. 点击查看效果 <a href="http://deer ...
- vmware 中ubuntu客户机 安装vmware tool vmhgfs 共享文件夹失败处理
vmware版本:10.0.0 build-1295980 ubuntu版本:3.13.0-62-generic 先安装的vmware workstation自带光盘中vmare tools包,安装完 ...
- 谈谈iOS中的屏幕方向
众所周知,iOS中提供了[UIDevice currentDevice].orientation与[UIApplication sharedApplication].statusBarOrientat ...
- 数据分析(7):pandas介绍和数据导入和导出
前言 Numpy Numpy是科学计算的基础包,对数组级的运算支持较好 pandas pandas提供了使我们能够快速便捷地处理结构化数据的大量数据结构和函数.pandas兼具Numpy高性能的数组计 ...
- ctf汇总
IDF实验室:牛刀小试 IDF实验室:倒行逆施 linux shell 常用指令 汇编笔记 堆栈溢出
- Android下的屏幕适配
1080 100dp 300px720 100dp 200px 300px 1080px 比例是300/1080=0.277200px 720px
- NOIp 2016 总结
NOIp 2016 总结 -----YJSheep Day 0 对于考前的前一天,晚自习在复习图论的最短路和生成树,加深了图的理解.睡得比较早,养足精力明日再战. Day 1 拿到题目,先过一边,题目 ...
- install hdp 2.2 on ubuntu 14.04
http://www.swiss-scalability.com/2014/12/install-hdp-22-on-ubuntu-1404-trusty.html 在新加节点上运行 sed -e & ...
- css箭头
.aui-list-item-arrow:before { content: ''; width: 0.4rem; height: 0.4rem; position: absolute; top: 5 ...