~~~
public function xlstools()
{
//测试专用
/* $filename="./Public/Uploads/20180917/ddd.xlsx";
$arr=$this->xlsin($filename);
$this->insetgoodlist($arr);*/
$table = I('post.');
$config=array(
'exts'=>array('xlsx','xls'),
'rootPath'=>"./Public/",
'savePath'=>'Uploads/',
//'autoSub' => true,
'subName' => array('date','Ymd'),
);
if(!empty($_FILES['excel']['name'])){
$upload = new \Think\Upload($config);
//var_dump($upload);exit;
if (!$info=$upload->uploadone($_FILES['excel'])) {
$this->error($upload->getError());
}
//要导入的xls文件,位于根目录下的Public文件夹
$filename = $upload->rootPath.$info['savepath'].$info['savename'];
//dump($filename);exit();
//$filename="./Public/Uploads/20180917/5b9f58bc7de0c.xlsx";
$arr=$this->xlsin($filename);
$this->insetgoodlist($arr);
}else{
$this->error('请选择文件');
}
}
public function insetgoodlist($arr){
//dump($arr);exit();
$i = 0;
foreach($arr as $key=>$list){
$i++;
if ($key!=1) {
foreach($list as $k=>$v){
switch($k){
case 'A':
$k='gid';
break;
case 'B':
$k='title';
break;
case 'C':
$k='barcode';
break;
case 'D':
$k='addtime1';
break;
case 'E':
$k='store';
break;
case 'F':
$k='rjiage';
break;
case 'G':
$k='xjiage';
break;
case 'H':
$k='bianhao';
break;
case 'I':
$k='jingban';
break;
}
$map[$k]=$v;
//$map['addtime']=date('Y-m-d H:i:s',time());
$map['uid']='';
$map['uname']=$map['jingban'];
$map['addtime']=date("Y-m-d h:i:s",$map['addtime1']);
$update[$k]=$v;
$update['uid']='';
$update['addtime']=$map['addtime'];
$update['uname']=$map['jingban'];
$update['shijian']= $update['addtime'];
$update['sn']='H987'.time().$i;
$update['status']=2;
}
//dump($map);exit();
$ids= M('goodlist')->add($map);
//dump($ids);exit();
if ($ids) {
$update['xid']=$ids;
//dump($update);exit();
M('goodlists')->add($update);
}
}
}
}
public function xlsin($filename)
{
//导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
import("Org.Util.PHPExcel");
//创建PHPExcel对象,注意,不能少了\
$PHPExcel = new \PHPExcel();
//如果excel文件后缀名为.xls,导入这个类
import("Org.Util.PHPExcel.Reader.Excel5");
//如果excel文件后缀名为.xlsx,导入这下类
//import("Org.Util.PHPExcel.Reader.Excel2007");
//$PHPReader=new \PHPExcel_Reader_Excel2007();
$PHPReader = new \PHPExcel_Reader_Excel2007();
//载入文件
$PHPExcel = $PHPReader->load($filename);
//获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
$currentSheet = $PHPExcel->getSheet(0);
//获取总列数
$allColumn = $currentSheet->getHighestColumn();
//获取总行数
$allRow = $currentSheet->getHighestRow();
//循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
//从哪列开始,A表示第一列
for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
//数据坐标
$address = $currentColumn . $currentRow;
//$arr[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
if($address=='D'.$currentRow){
$date= $currentSheet->getCell($address)->getValue();
$stamp_date = \PHPExcel_Shared_Date::ExcelToPHP($date);//将获取的奇怪数字转成时间戳,该时间戳会自动带上当前日期
//$arr[$currentRow][$currentColumn] = gmdate("H:i:s",$stamp_date);//这个就是excel表中的数据了,棒棒的!
$arr[$currentRow][$currentColumn] = $stamp_date;
}else{
//读取到的数据,保存到数组$arr中
$arr[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
}
}
}
return $arr;
//var_dump($arr);exit();
}
~~~