企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 2、PHPExcel导出 ![](https://box.kancloud.cn/6675cfc338cb8a64c035e2143ca82730_437x153.png) ### html代码 ~~~ <form class="layui-form layui-col-md12 x-so" action="{:url('admin/fangwei/daochu')}" method="get"> <input type="text" name="fw_pici" value="{$fw_pici ? $fw_pici : '';}" placeholder="请输入印刷批次" class="layui-input"> <button class="layui-btn"><i class="layui-icon">导出</i></button> </form> ~~~ ### 控制器代码 ~~~ //导出xls public function daochu(){ //1.从数据库中取出数据 $where =input('param.fw_pici'); //halt($where); $list = Db('fangwei')->where('fw_pici','=',$where)->order('fw_id ASC')->select(); //halt($list); //2.加载PHPExcle类库 vendor('PHPExcel.PHPExcel'); //3.实例化PHPExcel类 $objPHPExcel = new \PHPExcel(); //4.激活当前的sheet表 $objPHPExcel->setActiveSheetIndex(0); //5.设置表格头(即excel表格的第一行) $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'ID') ->setCellValue('B1', '防伪码') ->setCellValue('C1', '标签码'); //设置A列水平居中 $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //设置单元格宽度 $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(30); //6.循环刚取出来的数组,将数据逐一添加到excel表格。 for($i=0;$i<count($list);$i++){ $objPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$list[$i]['fw_id']);//ID $objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$list[$i]['fw_biaoqianma']);//标签码 $objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$list[$i]['fw_fangweima']);//防伪码 } //7.设置保存的Excel表格名称 $filename = '印刷防伪码'.date('ymd',time()).'.xls'; //8.设置当前激活的sheet表格名称; $objPHPExcel->getActiveSheet()->setTitle('防伪码'); //9.设置浏览器窗口下载表格 header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header('Content-Disposition:inline;filename="'.$filename.'"'); //生成excel文件 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //下载文件在浏览器窗口 $objWriter->save('php://output'); exit; } ~~~