public function importData(){
//先执行文件上传
$file = $this->request->file('files');//获取表单上传文件
if(empty($file)){
$this->error('请选择上传文件');
}else{
//移动到框架应用根目录public/excel
$info = $file->move(ROOT_PATH . 'public' . DS . 'Excel');
if($info){
$file_name = ROOT_PATH . 'public' . DS . 'Excel' . DS . $info->getsaveName();
//判断导入表格后缀格式
$extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
if($extension == 'xlsx'){
//xlsx后缀
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
}else{
//xls后缀
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
}
//加载文件内容,编码utf-8
$obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8');
$excel_array = $obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
array_shift($excel_array); //删除第一个数组(标题);
foreach ($excel_array as $key => $value) {
//根据需求处理
}
}else{
//上传失败获取错误信息
$this->error($file->getError());
}
}
}