thinkphp3.2和phpexcel导入最基本用法
先整个最基础的代码,理解了这个,后面的就非常简单了$file_name= './Upload/excel/123456.xls';<br>
import("Org.Util.PHPExcel");<br>
import("Org.Util.PHPExcel.IOFactory");<br>
$objReader = \PHPExcel_IOFactory::createReader('Excel5');<br>
$objPHPExcel = $objReader->load($file_name,$encode='utf-8');<br>
$sheet = $objPHPExcel->getSheet(0);<br>
$highestRow = $sheet->getHighestRow(); // 取得总行数<br>
$highestColumn = $sheet->getHighestColumn(); // 取得总列数<br>
<br>
$s = $objPHPExcel->getActiveSheet()->getCell("A2")->getValue();
表格内容:
再给大家整个一点难度的,先说下思路。
1.上传excel文件,得到它的地址
2.写个处理exl的function,即可
实例代码演示:public function upload(){<br>
$files = $_FILES['exl'];<br>
<br>
<br>
// exl格式,否则重新上传<br>
if($files['type'] !='application/vnd.ms-excel'){<br>
$this->error('不是Excel文件,请重新上传'); <br>
}<br>
<br>
// 上传<br>
$upload = new \Think\Upload();// 实例化上传类<br>
$upload->maxSize = 3145728 ;// 设置附件上传大小<br>
$upload->exts = array('xls');// 设置附件上传类型<br>
$upload->rootPath = './Upload/'; // 设置附件上传根目录<br>
$upload->savePath = 'excel/'; // 设置附件上传(子)目录<br>
//$upload->subName = array('date', 'Ym');<br>
$upload->subName = '';<br>
// 上传文件 <br>
$info = $upload->upload();<br>
<br>
$file_name = $upload->rootPath.$info['exl']['savepath'].$info['exl']['savename'];<br>
$exl = $this->import_exl($file_name);<br>
<br>
// 去掉第exl表格中第一行<br>
unset($exl[0]);<br>
<br>
// 清理空数组<br>
foreach($exl as $k=>$v){<br>
if(empty($v)){<br>
unset($exl[$k]);<br>
} <br>
};<br>
// 重新排序<br>
sort($exl);<br>
<br>
$count = count($exl);<br>
// 检测表格导入成功后,是否有数据生成<br>
if($count
$this->error('未检测到有效数据'); <br>
}<br>
<br>
// 开始组合数据<br>
foreach($exl as $k=>$v){<br>
<br>
$goods[$k]['goods_sn'] = $v;<br>
<br>
// 查询数据库<br>
$where['goods_sn'] = array('like','%'.$v.'%');<br>
$res = M('goods')->where($where)->find();<br>
<br>
$goods[$k]['goods_name'] = $res['goods_name'];<br>
$goods[$k]['goods_thumb'] = $res['goods_thumb'];<br>
if($res){<br>
// 是否匹配成功 <br>
$goods[$k]['is_match'] = '1';<br>
$f += 1;<br>
}else{<br>
// 匹配失败<br>
$goods[$k]['is_match'] = '0';<br>
$w += 1;<br>
}<br>
<br>
}<br>
// 实例化数据<br>
$this->assign('goods',$goods);<br>
//print_r($f);<br>
<br>
// 统计结果<br>
$total['count'] = $count;<br>
$total['success'] = $f;<br>
$total['error'] = $w;<br>
$this->assign('total',$total);<br>
<br>
// 删除Excel文件<br>
unlink($file_name);<br>
$this->display('info');<br>
<br>
}<br>
/* 处理上传exl数据<br>
* $file_name 文件路径<br>
*/<br>
public function import_exl($file_name){<br>
//$file_name= './Upload/excel/123456.xls';<br>
import("Org.Util.PHPExcel"); // 这里不能漏掉<br>
import("Org.Util.PHPExcel.IOFactory");<br>
$objReader = \PHPExcel_IOFactory::createReader('Excel5');<br>
$objPHPExcel = $objReader->load($file_name,$encode='utf-8');<br>
$sheet = $objPHPExcel->getSheet(0);<br>
$highestRow = $sheet->getHighestRow(); // 取得总行数<br>
$highestColumn = $sheet->getHighestColumn(); // 取得总列数<br>
<br>
for($i=1;$i
$data[] = $objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue(); <br>
}<br>
return $data; <br>
}
thinkphp3.2和phpexcel带生成图片完美案例
http://www.thinkphp.cn/code/2112.html
云栖大会北京站:阿里技术专家难得出镜,这次一下来了100多位?!