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 ...
随机推荐
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- xml in SQL
几年前,学习html时,顺便把xml也学了哈,知道了xpath和xquery的概念,可都没去落实,下面这篇文章,可以学习学习 http://www.cnblogs.com/huyong/archive ...
- Delphi中ExtractFilePath、ParamStr以及更多文件/目录操作涉及的函数。附加对应的例子
先介绍ExtractFilePath和ParamStr ParamStr 该函数的原型是:function paramstr(i: Integer): String; 对于任何的application ...
- PyAutoGUI 简介
转载来自: https://muxuezi.github.io/posts/doc-pyautogui.html http://blog.topspeedsnail.com/archives/5373 ...
- V4.0到来了,css雪碧图生成工具4.0更新啦
V3.0介绍 http://www.cnblogs.com/wang4517/p/4476758.html V4.0更新内容 V4.0下载地址:http://download.csdn.net/det ...
- Python-匿名函数
lambda 函数是一种快速定义单行的最小函数,可以用在任何需要函数的地方 常规版本: def fun(x,y) return x*y lambda版本: r = lambda x,y:x*y p ...
- html传值 location.search取
$(function() { var url = decodeURI(location.search); if (url.indexOf("?") != -1) { var str ...
- IOS UIAppLocation 单例模式
UIApplocation * app=[UIApplocation shareapplocation]; UIAppLocation 只能被初始化一次. 一个程序中只能被创建一次,称为单例模式. 单 ...
- C++常用特性原理解析
在我的早期印象中,C++这门语言是软件工程发展过程中,出于对面向对象语言级支持不可或缺的情况下,一群曾经信誓旦旦想要用C统治宇宙的极客们妥协出来的一个高性能怪咖. 它驳杂万分,但引人入胜,出于多(mi ...
- DOM性能瓶颈与Javascript性能优化
这两天比较闲,写了两篇关于JS性能缺陷与解决方案的文章(<JS特性性能缺陷及JIT的解决方案>,<Javascript垃圾回收浅析>),主要描述了untyped,GC带来的问题 ...