本例以thinkphp5.1為例
包地址:
https://packagist.org/packages/phpoffice/phpexcel
使用:
composer require phpoffice/phpexcel
控制器引入
//引入phpoffice use PHPExcel; use PHPExcel_IOFactory;
導出代碼:
//execl模板下載 public function template_download() { $objExcel = new PHPExcel(); $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5'); $objActSheet = $objExcel->getActiveSheet(0); $objActSheet->setTitle('會員批量導入模板'); //設置excel的標題 $objActSheet->setCellValue('A1', '用戶id'); $objActSheet->setCellValue('B1', '昵稱'); $objActSheet->setCellValue('C1', '手機號'); $baseRow = 2; //數據從N-1行開始往下輸出 這里是避免頭信息被覆蓋 //默認數據 $explame_data = array( array( 'user_id' => '1', 'nickname' => '小明', 'phone' => '15012345678', ), ); foreach ($explame_data as $key => $value) { $i = $baseRow + $key; $objExcel->getActiveSheet()->setCellValue('A' . $i, $value['user_id']); $objExcel->getActiveSheet()->setCellValue('B' . $i, $value['nickname']); $objExcel->getActiveSheet()->setCellValue('C' . $i, $value['phone']); } $objExcel->setActiveSheetIndex(0); //4、輸出 $objExcel->setActiveSheetIndex(); header('Content-Type: applicationnd.ms-excel'); $time = date('Y-m-d'); header("Content-Disposition: attachment;filename=會員批量導入模板" . $time . ".xls"); header('Cache-Control: max-age=0'); $objWriter->save('php://output'); }
導入代碼:
public function import_batch_send() { header("content-type:text/html;charset=utf-8"); //上傳excel文件 $file = request()->file('file'); //將文件保存到public/uploads目錄下面 $info = $file->validate(['size' => 1048576, 'ext' => 'xls,xlsx'])->move('./uploads'); if ($info) { //獲取上傳到后台的文件名 $fileName = $info->getSaveName(); //獲取文件路徑 $filePath = Env::get('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $fileName; //獲取文件后綴 $suffix = $info->getExtension(); //判斷哪種類型 if ($suffix == "xlsx") { $reader = \PHPExcel_IOFactory::createReader('Excel2007'); } else { $reader = PHPExcel_IOFactory::createReader('Excel5'); } } else { return json(['status' => '1', 'message' => '文件過大或格式不正確導致上傳失敗-_-!']); } //載入excel文件 $excel = $reader->load($filePath, $encode = 'utf-8'); //讀取第一張表 $sheet = $excel->getSheet(0); //獲取總行數 $row_num = $sheet->getHighestRow(); //獲取總列數 $col_num = $sheet->getHighestColumn(); $import_data = []; //數組形式獲取表格數據 for ($i = 2; $i <= $row_num; $i++) {
$import_data[$i]['nickname'] = $sheet->getCell("B" . $i)->getValue(); $import_data[$i]['phone'] = $sheet->getCell("C" . $i)->getValue(); } if (empty($import_data)) { return json(['status' => '1', 'message' => '數據解析失敗']); } //校驗手機號是否重復 $phone_array = array_column($import_data, 'phone'); $phone_ids = implode(',', $phone_array); $result_phone = db('user') ->field('phone') ->where('phone', 'in', $phone_ids) ->select(); if (!empty($result_phone)) { $result_phone_array = array_column($result_phone, 'phone'); $result_phone_ids = implode(',', $result_phone_array); return json(['status' => '3', 'message' => '數據重復', 'result' => $result_phone_ids]); } //將數據保存到數據庫 $res = db('user')->insertAll($import_data); if ($res) { return json(['status' => '2', 'message' => '導入成功']); } else { return json(['status' => '1', 'message' => '提交失敗,請刷新重試']); } }
提示: 注意官方庫給的環境要求以及需要開啟的擴展