💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### 第一步:引入类库,下面的没有用到的就直接注释就好了,用的是PhpOffice的库 ```php use app\admin\model\MyFund as MyFundModel; use app\common\controller\Backend; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Reader\Csv; use PhpOffice\PhpSpreadsheet\Reader\Xls; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use think\Db; use think\Exception; use think\exception\PDOException; use think\exception\ValidateException; use think\Loader; use \PhpOffice\PhpSpreadsheet\Spreadsheet; use \PhpOffice\PhpSpreadsheet\IOFactory;//use \PHPExcel_Style_NumberFormat; //设置列的格式==>>设置文本格式 ``` ### 第二步:导入代码 ```php /*导出*/ public function exports(){ $adminList=\app\admin\model\Orders::where(['isjieyong'=>1])->select(); $newExcel = new Spreadsheet(); //创建一个新的excel文档 $objSheet = $newExcel->getActiveSheet(); //获取当前操作sheet的对象 //设置宽度为true,不然太窄了 $newExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true); $newExcel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true); //设置第一栏的标题 $objSheet ->setCellValue('A1', '会员id') ->setCellValue('B1', '保险名称') ->setCellValue('C1', '订单编号') ->setCellValue('D1', '保单号') ->setCellValue('E1', '生效日期') ->setCellValue('F1', '终止日期') ->setCellValue('G1', '保费') ->setCellValue('H1', '保额') ->setCellValue('I1', '比例') ->setCellValue('J1', '缴费年限') ->setCellValue('K1', '投保人信息') ->setCellValue('L1', '被保人信息') ->setCellValue('M1', '受益人'); //第二行起,每一行的值,setCellValueExplicit是用来导出文本格式的。 //->setCellValueExplicit('C' . $k, $val['admin_password']PHPExcel_Cell_DataType::TYPE_STRING),可以用来导出数字不变格式 foreach ($adminList as $k => $val) { $k = $k + 2; $mobileinfo=\app\admin\model\User::get($val['user_id']); $baoxianinfo=\app\admin\model\Maindata::get($val['maindata_id']); $objSheet ->setCellValue('A' . $k, $mobileinfo['mobile']) ->setCellValue('B' . $k, $baoxianinfo['back_product_name']) ->setCellValue('C' . $k, $val['order_no']) ->setCellValue('D' . $k, $val['gua_slip']) ->setCellValue('E' . $k,$val['effective_date']) ->setCellValue('F' . $k, $val['end_time']) ->setCellValue('G' . $k, $val['coverage']) ->setCellValue('H' . $k, $val['sum_insured']) ->setCellValue('I' . $k, $val['ratio']) ->setCellValue('J' . $k, $val['payment_year']) ->setCellValue('K' . $k, json_encode($val['insure_info'],JSON_UNESCAPED_UNICODE)) ->setCellValue('L' . $k, json_encode($val['insured_info'],JSON_UNESCAPED_UNICODE)) ->setCellValue('M' . $k, $val['beneficiary']); } $this->downloadExcel($newExcel, date('YmsHis',time()).time(), 'Xls'); } ``` ### 第三步:公共文件,用来传入xls并下载 ```php public function downloadExcel($newExcel, $filename, $format) { // $format只能为 Xlsx 或 Xls if ($format == 'Xlsx') { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); } elseif ($format == 'Xls') { header('Content-Type: application/vnd.ms-excel'); } header("Content-Disposition: attachment;filename=" . $filename . date('Y-m-d') . '.' . strtolower($format)); header('Cache-Control: max-age=0'); $objWriter = IOFactory::createWriter($newExcel, $format); $objWriter->save('php://output'); exit; } ```