配置php错误导入文件,php 导入文件(逻辑处理)

基于TP 5.0

/*

*导入平板

*/

public function loaddevice(){

set_time_limit(0);

//header('Content-Type: text/html; charset=utf-8'); //网页编码

//header('Content-Type:application/json; charset=utf-8');

Loader::import('PHPExcel.Classes.PHPExcel');

Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');

Loader::import('PHPExcel.Classes.PHPExcel.Reader.Excel5');

//获取表单上传文件

//writeLog('file1=======' . var_export($_FILES,true));

//writeLog('file2=======' . var_export(request()->file('file'),true));

//$file = $_FILES['file'];

$file = request()->file('file');

$info = $file->validate(['ext' => 'xlsx,xls'])->move(ROOT_PATH . 'public' . DS . 'uploads');//上传验证后缀名,以及上传之后移动的地址

if (!$info) {

$msg['status'] = 0;

$msg['msg'] = $file->getError(); // 上传错误提示错误信息

return json($msg);

}

// echo $info->getFilename();die;

$exclePath = $info->getSaveName(); //获取文件名

$file_name = ROOT_PATH . 'public' . DS . 'uploads' . DS . $exclePath; //上传文件的地址

$objReader =\PHPExcel_IOFactory::createReader('Excel2007');

$objPHPExcel = $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8

//echo "

";

// $excel_array=$obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式

// array_shift($excel_array); //删除第一个数组(标题);

// $city = [];

// //pr($excel_array);

// foreach($excel_array as $k=>$v) {

// $city[$k]['macno'] = $v[0];

// }

//Db::name('device')->insertAll($city); //批量插入数据

$sheet =$objPHPExcel->getSheet(0);

$highestRow = $sheet->getHighestRow();//取得总行数

$highestColumn =$sheet->getHighestColumn(); //取得总列数

$error = "";

//pr($highestRow);

for ($i = 2; $i <= $highestRow; $i++) {

//看这里看这里,前面小写的a是表中的字段名,后面的大写A是excel中位置

$data['macno'] =$objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();

$deviceInfo = db('device')->where(['macno'=>$data['macno']])->find();

if($deviceInfo){

$error .= ','.$i;

}else if($data['macno']){

$data['ctime'] = time();

$res = db('device')->insert($data);

}else{

$error .= ','.$i;

}

}

@unlink($file_name);

$data1['status'] = 1;

if($error){

$data1['msg'] = '第'.trim($error,',').'已经存在';

}else{

$data1['msg'] = '全部导入成功!';

}

return json($data1);

}

基于TP 3.2

/*

*导入设备

*/

public function loaddevice(){

set_time_limit(0);

$upload = new \Think\Upload(); // 实例化上传类

$upload->maxSize = 10485760 ; // 设置附件上传大小

$upload->exts = array('xls','xlsx'); // 设置附件上传类型

$upload->rootPath = './Upload/'; // 设置附件上传根目录

$upload->autoSub = false;

$info = $upload->upload(); // 上传文件

$exts = $info['file']['ext']; // 获取文件后缀

$file_name = $upload->rootPath.$info['file']['savename']; // 生成文件路径名

if(!$info) {

$msg['status'] = 0;

$msg['msg'] = $upload->getError(); // 上传错误提示错误信息

$this->ajaxReturn($msg);

}

vendor("PHPExcel.PHPExcel");

if ($exts == 'xlsx') {

$objReader =\PHPExcel_IOFactory::createReader('Excel2007');

$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');

} else if ($exts == 'xls'){

$objReader =\PHPExcel_IOFactory::createReader('Excel5');

$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');

}

$sheet =$objPHPExcel->getSheet(0);

$highestRow = $sheet->getHighestRow();//取得总行数

$highestColumn =$sheet->getHighestColumn(); //取得总列数

$error = "";

// $result=[

// ['macno'=>'20190113','number'=>1,'type'=>1],

// ['macno'=>'20190112','number'=>2,'type'=>1],

// ];

for ($i = 2; $i <= $highestRow; $i++) {

//看这里看这里,前面小写的a是表中的字段名,后面的大写A是excel中位置

$data['macno'] =$objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();

$data['number'] =$objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue();

$data['type'] =$objPHPExcel->getActiveSheet()->getCell("C" . $i)->getValue();

$deviceInfo = M('device')->where(['macno'=>$data['macno']])->find();

if($deviceInfo){

$error .= ','.$i;

}else if($data['macno']&&$data['number']&&$data['type']){

$data['ctime'] = time();

$res = M('device')->add($data);

for ($i=1; $i <=$data['number']; $i++) {

$da['macno'] = $data['macno'];

$da['hatch'] = $i;

$da['createat'] = time();

$res = M('device_hatch')->add($da);

}

}else{

$error .= ','.$i;

}

}

@unlink($file_name);

$data1['status'] = 1;

if($error){

$data1['msg'] = '第'.trim($error,',').'已经存在';

}else{

$data1['msg'] = '全部导入成功!';

}

$this->ajaxReturn($data1);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值