getActiveSheet();
$letter = array('A','B','C','D');
//表头数组
$tableheader = array(
'MAC地址','手机品牌','采集地址','采集时间'
);
//填充表头信息
for($i = 0;$i < count($tableheader);$i++) {
$PHPSheet->setCellValue("$letter[$i]1","$tableheader[$i]");
}
$where = array();
//获取当前账号id()
$id = $this->user_id;
$datas = $this->request->param();
$res = $this->validate($datas,"Index.search");
if($res){
$st_time = strtotime(input('st_time'));//下单开始时间
$end_time = strtotime(input('end_time'));//下单结束时间
$device_name = input('device_name');//设备名称
$wifi_name = input('wifi_name'); //wifi名称
$brand_name = input("brand_name");//手机品牌名称
$unknow = $datas['unknow'] ?? '1'; //品牌是否为未知数据
// dump($device_info);
$this->assign(array(
'st_time' => input('st_time'),
'end_time' => input('end_time'),
'device_name' => $device_name,
'wifi_name' => $wifi_name,
'brand_name' => $brand_name,
'unknow' => $unknow,
));
//时间查询
// //-------------------------------------
if($st_time && !$end_time){
//在xx时间后
$where['createtime'] = array('egt',$st_time);
}elseif($end_time && !$st_time){
//在xx时间前
$where['createtime'] = array('elt',$end_time);
}elseif($end_time && $st_time){
//在xx-yy时间段
if($st_time>$end_time){
$this->error("开始时间不能大于结束时间",url("data/index/index"));
}else{
$ends_time=strtotime('+1 day',$end_time);
$where['createtime'] = array('between',array($st_time,$ends_time));
}
}
// //-------------------------------
//设备名称
if(!empty($device_name)){
$where['u_id'] = array('=',$device_name);
}
//wifi名称
if(!empty($wifi_name)){
$where['wifi_name'] = array('like',"%$wifi_name%");
}
//手机品牌名称
if(!empty($brand_name)){
$where['phone_brand'] = array('like',"%$brand_name%");
}
//unknow数据表
if($unknow != 1){
$unkn = '_unknow';
}else{
$unkn = '';
}
}else{
$this->error($res);
}
// //判断权限;
if($id != '1'){
//不是管理员 只能获取自己的数据
$where['user_id'] = array('=',$id);
}
//查询数据集
//------------------------------------
//分表规则 : user_id的尾数与表名数字一致
$table = 'mac_data.cmf_mac_data_'.substr($this->user_id,-1).$unkn;
$data = Db::table($table)
->where($where)
->where('delete_time','null')
->order('createtime desc')
->field('mac_info,phone_brand,addr,FROM_UNIXTIME(createtime,"%Y-%m-%d %H:%i:%S")as time')
->select();
//填充表格信息
$count=count($data)+1;
for ($i = 2;$i <= $count;$i++) {
//i=行数 j=排数
$j = 0;
foreach ($data[$i - 2] as $key=>$value) {
//$i=2 是因为数据从第二排开始
$PHPSheet->setCellValue("$letter[$j]$i","$value");
$j++;
}
}
$PHPSheet->setTitle("data"); //给当前活动sheet设置名称
$PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007");
header('Content-Disposition: attachment;filename="导出数据.xlsx"');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
}
//导入界面
public function addexcel()
{
return $this->fetch('index/addexcel');
}
//导入excel
public function inserExcel()
{
Loader::import('Classes.PHPExcel');
Loader::import('Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
Loader::import('Classes.PHPExcel.Reader.Excel7');
//获取表单上传文件
$file = request()->file('file');
if(empty($file)){
$this->error('未检测到上传文件!');
}
$info = $file->validate(['size'=>2097152,'ext'=>'csv'])->move(ROOT_PATH . 'public' . DS . 'upload/excel');
if(!$info){
$this->error($file->getError());
}
//施展终极空间忍术·移动文件
$exclePath = '/excel/'.$info->getSaveName(); //获取文件名
$file_name = ROOT_PATH . 'public' . DS . 'upload' . DS . $exclePath; //上传文件的地址
$objReader =\PHPExcel_IOFactory::createReader('CSV')->setInputEncoding('GBK');
$obj_PHPExcel =$objReader->load($file_name); //加载文件内容,编码utf-8
$excel_array=$obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
array_shift($excel_array); //删除第一个数组(标题);
$city = [];
$num = 0;
$table = 'mac_data.cmf_mac_phones';
//处理内容
$insert_data = array();
foreach($excel_array as $k=>$v) {
if($v[2] == null){
continue; //过滤空数据
}
$are = explode(' ',$v[5]);
$city = $are[1] ?? $are[0];
$province = $are[0];
$phone = explode(' ',$v[2]);
if(count($phone)>1){
foreach($phone as $val){
$insert_data[] = array(
'name' => $v[1],
'mobile' => $val,
'area' => $v[4],
'coord' => $v[8],
'city' => $city,
'search' => $v[6],
'province' =>$province,
);
$num++;
}
}else{
$insert_data[] = array(
'name' => $v[1],
'mobile' => $v[2],
'area' => $v[4],
'coord' => $v[8],
'city' => $city,
'search' => $v[6],
'province' =>$province,
);
$num++;
}
}
//dump($insert_data);die;
$zxc = Db::table($table)->fetchSql(true)->insertAll($insert_data,true);
if(!$zxc){
$this->error('导入失败');
}
trace($zxc);
$this->success('共成功导入'.$num ."条数据!");
}
//导入模板
public function demo()
{
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
die;
Loader::import('Classes.PHPExcel');
Loader::import('Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
$PHPExcel = new \PHPExcel();
$PHPSheet = $PHPExcel->getActiveSheet();
$letter = array('A','B','C','D');
//表头数组
$tableheader = array(
'订单编号','物流公司','物流单号','支持物流(顺丰速运,百世快递,中通快递,申通快递,圆通速递,韵达速递,邮政快递包裹,MS,天天快递,京东物流,优速快递,德邦快递,快捷快递,宅急送,TNT快递)'
);
//填充表头信息
for($i = 0;$i < count($tableheader);$i++) {
$PHPSheet->setCellValue("$letter[$i]1","$tableheader[$i]");
}
$PHPExcel->getActiveSheet()->getStyle('A')->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
$PHPExcel->getActiveSheet()->getStyle('C')->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
$data = array(
'10000000000XXXX','圆通速递','XXXXXXXXXXX'
);
$j = 0;
foreach ($data as $value) {
$PHPSheet->setCellValue("$letter[$j]2","$value");
$j++;
}
$PHPSheet->setTitle("批量发货模板"); //给当前活动sheet设置名称
$PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007");
header('Content-Disposition: attachment;filename="demo.xlsx"');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
}
}