tp5-批量导入数据

添加页面



    
    
        

 
浏览

controller

use PHPExcel_IOFactory;
use PHPExcel;
class Student{

public function addstudents() 
   {
        $request = \think\Request::instance();
        $file = $request->file('excel');

        $save_path = ROOT_PATH . 'uploads/';
        $info = $file->move($save_path);

        $filename=$save_path . DIRECTORY_SEPARATOR . $info->getSaveName();
	    if(file_exists($filename)) 
	    {
          	 	Vendor('phpexcel.PHPExcel');
				$phpExcel = PHPExcel_IOFactory::load($filename);
				 
				$phpExcel->setActiveSheetIndex(0);
				// 获取行数
				$row = $phpExcel->getActiveSheet()->getHighestRow();

	           $sheet = $phpExcel->getActiveSheet(0);//获得sheet

	           $total = 0;

			 	for($j=2;$j<$row;$j++){
			        //从A列读取数据
			        $data = array();
			        $data['sno'] = $sheet->getCell("A$j")->getValue();
			        $data['name'] = $sheet->getCell("B$j")->getValue();
			        $data['phone'] = $sheet->getCell("C$j")->getValue();
			        $data['class'] = $sheet->getCell("D$j")->getValue();
			        $data['academy'] = $sheet->getCell("E$j")->getValue();
			        $data['mac_address'] = $sheet->getCell("F$j")->getValue();
			        $num = db('student')->insert($data);
			        $total += $num;
			    }
				$this->success("成功导入{$total}条数据!",url('studentlist'));				
		}
   
   }
}

在vendor 引入 PHPExcel 即 PHPExcel 官网下载后的classes 文件可更名为phpexcel 复制到 vendor

tp5-批量导入数据_第1张图片

图片:

tp5-批量导入数据_第2张图片

你可能感兴趣的:(Tp5)