先整个最基础的代码,理解了这个,后面的就非常简单了

$file_name= './Upload/excel/123456.xls';
import("Org.Util.PHPExcel");
import("Org.Util.PHPExcel.IOFactory");
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
$sheet = $objPHPExcel->getSheet();
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数 $s = $objPHPExcel->getActiveSheet()->getCell("A2")->getValue();

再给大家整个一点难度的,先说下思路。
1.上传excel文件,得到它的地址
2.写个处理exl的function,即可
实例代码演示:

public function upload(){
$files = $_FILES['exl']; // exl格式,否则重新上传
if($files['type'] !='application/vnd.ms-excel'){
$this->error('不是Excel文件,请重新上传');
} // 上传
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = ;// 设置附件上传大小
$upload->exts = array('xls');// 设置附件上传类型
$upload->rootPath = './Upload/'; // 设置附件上传根目录
$upload->savePath = 'excel/'; // 设置附件上传(子)目录
//$upload->subName = array('date', 'Ym');
$upload->subName = '';
// 上传文件
$info = $upload->upload(); $file_name = $upload->rootPath.$info['exl']['savepath'].$info['exl']['savename'];
$exl = $this->import_exl($file_name); // 去掉第exl表格中第一行
unset($exl[]); // 清理空数组
foreach($exl as $k=>$v){
if(empty($v)){
unset($exl[$k]);
}
};
// 重新排序
sort($exl); $count = count($exl);
// 检测表格导入成功后,是否有数据生成
if($count<){
$this->error('未检测到有效数据');
} // 开始组合数据
foreach($exl as $k=>$v){ $goods[$k]['goods_sn'] = $v; // 查询数据库
$where['goods_sn'] = array('like','%'.$v.'%');
$res = M('goods')->where($where)->find(); $goods[$k]['goods_name'] = $res['goods_name'];
$goods[$k]['goods_thumb'] = $res['goods_thumb'];
if($res){
// 是否匹配成功
$goods[$k]['is_match'] = '';
$f += ;
}else{
// 匹配失败
$goods[$k]['is_match'] = '';
$w += ;
} }
// 实例化数据
$this->assign('goods',$goods);
//print_r($f); // 统计结果
$total['count'] = $count;
$total['success'] = $f;
$total['error'] = $w;
$this->assign('total',$total); // 删除Excel文件
unlink($file_name);
$this->display('info'); }
/* 处理上传exl数据
* $file_name 文件路径
*/
public function import_exl($file_name){
//$file_name= './Upload/excel/123456.xls';
import("Org.Util.PHPExcel"); // 这里不能漏掉
import("Org.Util.PHPExcel.IOFactory");
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
$sheet = $objPHPExcel->getSheet();
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数 for($i=;$i<$highestRow+;$i++){
$data[] = $objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();
}
return $data;
}

http://www.thinkphp.cn/code/2124.html

thinkphp3.2和phpexcel导入的更多相关文章

  1. phpexcel导入数据库 基于thinkphp3.2

    public function studentImportExcel(){ if (!empty ( $_FILES)){ $upload = new \Think\Upload();        ...

  2. ThinkPhp3.2.3 使用phpExcel导入数据

    public function uploadExcel(){ if (!empty($_FILES)){ $upload = new \Think\Upload(); // 实例化上传类 $uploa ...

  3. thinkphp3.2与phpexcel带图片生成 完美案例

    thinkphp3.2与phpexcel完美案例 // 导出exl public function look_down(){ $id = I('get.id'); $m = M ('offer_goo ...

  4. thinkphp phpexcel导入

    上次做了一个基于tp3.2.3的phpexcel导出,这次是phpexcel导入,准备材料phpexcel(不知道下载地址的查看我上一篇博文),虽说是基于thinkphp3.2.3来的,也只不过是引入 ...

  5. phpexcel导入数据提示失败

    phpexcel导入excel时明明只有几行数据,却提示506行失败,原来是excel中有506行"无效数据"(看起来是空的,但是和没有数据不一样).

  6. Thinkphp 用PHPExcel 导入Excel

    搞了个简单的Excel导入, 用的是PHPExcel(百科:用来操作Office Excel文档的一个PHP类库, 基于微软的OpenXML标准和PHP语言) 好, 不说了, 开始吧... 首先得有P ...

  7. thinkphp中使用PHPEXCEL导入数据

    导入方法比较简单 但必须考虑到Excel本身单元格格式问题 例如以0开头的字符串读出来被去掉了前导0 成为float型而丢失一位 必须进行处理 <?php /** * Author lizhao ...

  8. 使用PHPExcel导入导出excel格式文件

    使用PHPExcel导入导出excel格式文件  作者:zccst  因为导出使用较多,以下是导出实现过程.  第一步,将PHPExcel的源码拷贝到项目的lib下  文件包含:PHPExcel.ph ...

  9. ThinkPHP第二十三天(Category表结构、PHPExcel导入数据函数)

    1.category分类表表结构id,name,pid,sort,结合category.class.php类使用. 2.PHPExcel导入数据函数示例 function excel_to_mysql ...

随机推荐

  1. EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态

    本文目录 查看实体当前.原始和数据库值:DbEntityEntry 查看实体的某个属性值:GetValue<TValue>方法 拷贝DbPropertyValues到实体:ToObject ...

  2. 父容器利用opacity设置透明后,子元素跟着变透明的解决方案

    背景半透明,子元素不透明的效果经常需要用到.通常对父容器使用opacity属性时,子元素也跟着变透明,所以不妨设置父容器的 background-color:rgba(r,g,b,x); 其中x取值从 ...

  3. 【GSM】GTM900C的应用——短信

    虽说GSM已经很老旧,但其低廉的价格,非常适合一些需要小数据上网传输和短信等功能的应用场合. 不知道GSM能否像51单片机一样,在低端应用中长久不衰.GTM900C发送短信,支持两种模式,TXT和PD ...

  4. CODE[VS]4633Mz树链剖分练习

    Description 给定一棵结点数为n的树,初始点权均为0,有依次q个操作,每次操作有三个参数a,b,c,当a=1时,表示给b号结点到c号结点路径上的所有点(包括b,c,下同)权值都增加1,当a= ...

  5. codevs 1052 地鼠游戏

    1052 地鼠游戏 http://codevs.cn/problem/1052/ 题目描述 Description 王钢是一名学习成绩优异的学生,在平时的学习中,他总能利用一切时间认真高效地学习,他不 ...

  6. 解读ASP.NET 5 & MVC6系列(9):日志框架

    框架介绍 在之前的.NET中,微软还没有提供过像样的日志框架,目前能用的一些框架比如Log4Net.NLog.CommonLogging使用起来多多少少都有些费劲,和java的SLF4J根本无法相比. ...

  7. [LeetCode] Island Perimeter 岛屿周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  8. [LeetCode] Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  9. [LeetCode] UTF-8 Validation 编码验证

    A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte char ...

  10. 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\106f9ae8\cc0e1

    在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件"c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.Ne ...