模板下载就是放一个文档
<a class="btn btn-primary" href="/upload/excel_tpl/导入摊位模板下载.xlsx" download="">摊位模板下载</a>
public function upload()
{
//获取表格的大小,限制上传表格的大小5M
$file_size = $_FILES['file']['size'];
if ( $file_size > 5 * 1024 * 1024 ) {
$this->error('文件大小不能超过5M');
}
$marketId = $this->marketIdAndCompanyId();
//限制上传表格类型
$fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
//application/vnd.ms-excel 为xls文件类型
if ( $fileExtendName != 'xlsx' ) {
$this->error('必须为excel表格,且必须为xlsx格式!');
// exit();
}
if ( is_uploaded_file($_FILES['file']['tmp_name']) ) {
// 有Xls和Xlsx格式两种
$objReader = IOFactory::createReader('Xlsx');
// $objReader= new Xls();
$filename = $_FILES['file']['tmp_name'];
$objPHPExcel = $objReader->load($filename); //$filename可以是上传的表格,或者是指定的表格
$sheet = $objPHPExcel->getSheet(0); //excel中的第一张sheet
$highestRow = $sheet->getHighestRow(); // 取得总行数
// var_dump($highestRow);die;
$objReader->getReadEmptyCells(false);
// $highestColumn = $sheet->getHighestColumn(); // 取得总列数
// var_dump($highestRow);die;
$fail =0;
$total = $highestRow -2;
for ($j = 3; $j <= $highestRow; $j++) {
$stall_number = $objPHPExcel->getActiveSheet()->getCell("A" . $j)->getFormattedValue();
$floor_name = $objPHPExcel->getActiveSheet()->getCell("B" . $j)->getFormattedValue();
$area_name = $objPHPExcel->getActiveSheet()->getCell("C" . $j)->getFormattedValue();
$floor = model('stall_category')->where(['type'=>'楼层','market_id'=>$marketId['market_id'],'name'=>$floor_name])->field('id')->find();
$area = model('stall_category')->where(['type'=>'业态','market_id'=>$marketId['market_id'],'name'=>$area_name])->field('id')->find();
if(empty($floor) || empty($area)){
$fail++;
file_put_contents($marketId['market_id']."upload_stall导入失败的摊位.txt", "时间:".date('Y-m-d H:i:s')."市场:".$marketId['market_id']."stall_number:".$stall_number.'floor_id:'.''.'area_id:'.''.PHP_EOL, FILE_APPEND);
continue;
}
if ($stall_number && $floor_name && $area_name){
$stall_is_exists = model('stall')->where('market_id',$marketId['market_id'])->where(['stall_number'=>$stall_number,'floor_id'=>$floor->id,'area_id'=>$area->id])->find();
if($stall_is_exists){
$fail++;
file_put_contents($marketId['market_id']."upload_stall导入失败的摊位.txt", "时间:".date('Y-m-d H:i:s')."市场:".$marketId['market_id']."stall_number:".$stall_number.'floor_id:'.$floor->id.'area_id:'.$area->id.PHP_EOL, FILE_APPEND);
continue;
}
$data =[
'stall_number'=>$stall_number,
'floor_id'=>$floor->id,
'area_id'=>$area->id,
'market_id'=>$marketId['market_id'],
];
model('stall')::create($data);
file_put_contents($marketId['market_id']."upload_stall导入成功的摊位.txt", "时间:".date('Y-m-d H:i:s')."市场:".$marketId['market_id']."stall_number:".$stall_number.'floor_id:'.$floor->id.'area_id:'.$area->id.PHP_EOL, FILE_APPEND);
}else{
file_put_contents($marketId['market_id']."upload_stall导入失败的摊位.txt", "时间:".date('Y-m-d H:i:s')."市场:".$marketId['market_id']."stall_number:".$stall_number.'floor_id:'.$floor['id'].'area_id:'.$area['id'].PHP_EOL, FILE_APPEND);
continue;
}
}
$this->success('导入成功,总条数为:'.$total.'失败了:'.$fail.'条');
}else{
$this->error('文件过大或格式不正确导致上传失败-_-!');
}
}