- public function excelInsert()
- {if($this->request->isPost())
- {
- $filestr = $this->request->param();
- Loader::import('PHPExcel',EXTEND_PATH);
- //Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
- Loader::import('PHPExcel.PHPExcel.IOFactory',EXTEND_PATH);
- Loader::import('PHPExcel.PHPExcel.Reader.Excel5',EXTEND_PATH);
- $file = request()->file('file');
- $info = $file->validate(['size'=>156780,'ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
- if ($info)
- {
- //echo $info->getFilename();
- $exclePath = $info->getSaveName(); // 获取文件名
- $file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $exclePath;
- $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
- $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8'); // 加载文件内容, 编码 utf-8
- $excel_array=$obj_PHPExcel->getsheet(0)->toArray(); // 转换为数组格式
- array_shift($excel_array);
- $data = [];
- foreach($excel_array as $k=>$v)
- {
- $data[$k]['price'] = isset($v[16])==true ? $v[16] : '';
- $data[$k]['group'] = isset($v[17])==true ? $v[17] : '';
- $data[$k]['flag'] = isset($v[18])==true ? $v[18] : '';
- $data[$k]['status'] = 1;
- }
- $rtn = Db::table('表')->insertAll($data); // 批量插入数据
- if($rtn)
- {
- $this->success('导入完成');
- }
- }
- else
- {
- echo $file->getError();
- }
- }
- }
类放在 extend 里
来源: http://www.bubuko.com/infodetail-2558366.html