本文实例讲述了thinkphp5.1 框架导入/导出excel文件操作。分享给大家供大家参考,具体如下:
thinkphp5.1 导入excel文件
public function importExcel() { try { //获取表格的大小,限制上传表格的大小 if ($_FILES['file']['size'] > 10 * 1024 * 1024) { //文件过大 log_debug($log_title . 'END === MSG:' . '文件过大'); parent::endBack(['state' => 0, 'msg' => '文件过大']); } //限制上传表格类型 $ext = substr(strrchr($_FILES['file']["name"], '.'), 1); if ($ext != 'xls' && $ext != 'xlsx') { log_debug($log_title . 'END === MSG:' . '文件格式不正确'); parent::endBack(['state' => 0, 'msg' => '上传文件必须为excel表格']); } //读取表格 $filename = $_FILES['file']['tmp_name']; $reader = IOFactory::createReader('Xlsx'); //Xls,Xlsx都可读取 $canRead = $reader->canRead($filename); if (!$canRead) { log_debug($log_title . 'END,文件格式不正确,SQL:' . Db::name('')->getLastSql()); parent::endBack(['state' => 0, 'msg' => '文件格式不正确', 're_login' => false]); } $spreadsheet = $reader->load($filename); //载入excel表格 $worksheet = $spreadsheet->getActiveSheet(); //选中sheet表 $highestRow = $worksheet->getHighestRow(); // 总行数 // $highestColumn = $worksheet->getHighestColumn(); // 总列数 if (!(0 < $highestRow)) { log_debug($log_title . 'END,文件内容空,SQL:' . Db::name('')->getLastSql()); parent::endBack(['state' => 0, 'msg' => '文件没有数据', 're_login' => false]); } //循环读取--有效判断 $sst_word_arr = []; //存放敏感词的数组 for ($row = 1; $row <= $highestRow; $row++) { //取列数A列的数据 $tmp_word = $spreadsheet->getActiveSheet()->getCell('A' . $row)->getValue(); if ('' != trim($tmp_word) && null != $tmp_word) { $sst_word_arr[] = $tmp_word; break; //发现有效数据,直接退出,接下来插入数据 } } // $sst_word_arr = array_unique($sst_word_arr); if (empty($sst_word_arr)) { log_debug($log_title . 'END,文件无有效数据,SQL:' . Db::name('')->getLastSql()); parent::endBack(['state' => 0, 'msg' => '文件无有效数据', 're_login' => false]); } //判断和数据库操作 for ($row = 2; $row <= $highestRow; $row++) { //取列数A列的数据 $tmp_old_car_num = $spreadsheet->getActiveSheet()->getCell('A' . $row)->getValue(); $car_num = trim($tmp_old_car_num); if ('' != $car_num && null != $car_num) { //数据库操作 } } } $ret_arr = [ 'state' => 1, //返回数据 ]; log_debug($log_title . 'END,SUCCESS'); parent::endBack($ret_arr); } catch (\Exception $e) { // } }
excel文件格式为:
thinkphp5.1 导出excel文件
namespase app\test; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; class test { public function carNumsExport() { $log_title = '测试 => 车牌列表导出[' . __METHOD__ . '] '; try { $file_name = '《车牌列表》from y8zh - ' . $user_info['uid'] . '.xlsx'; $file_relative_path = parent::$module_name . DIRECTORY_SEPARATOR . 'fcb_car_nums' . DIRECTORY_SEPARATOR; $file_path = parent::$api_file_root_path . $file_relative_path; // 已生成过则直接返回 if (file_exists($file_path . $file_name)) { $ret_arr = [ 'state' => 1, 'download_url' => parent::$api_file_get_url . $file_relative_path . $file_name, ]; parent::endBack($ret_arr); } if (!is_dir($file_path)) { mkdir($file_path, 0777, true); } $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); //获取所有车牌号 $car_nums = Db::connect('db_config_yun')->name('vechicle')->column('DISTINCT number'); $i = 1; $sheet->setCellValue('A' . $i, '车牌号')->getStyle('A' . $i)->getFont()->setBold(true); $i++; // 表内容 if (!empty($car_nums)) { foreach ($car_nums as $k_c => $v_c) { $sheet->setCellValue('A' . $i, $v_c); $i++; } } $writer = new Xlsx($spreadsheet); $writer->save($file_path . $file_name); $ret_arr = [ 'state' => 1, 'download_url' => parent::$api_file_get_url . $file_relative_path . $file_name, ]; log_debug($log_title . 'END === DOWNLOAD_URL:' . $ret_arr['download_url']); parent::endBack($ret_arr); } catch (\Exception $e) { // } } }
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题