public function excel_handle()
{
//1.连接数据库查询对应数据;
$list = Db::table('m_opgroup')->select();
//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', '职务')
->setCellValue('D1', '是否开启');
//设置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);
//6.循环刚取出来的数组,将数据逐一添加到excel表格。
for($i=0;$i<count($list);$i++){
if($list[$i]['upflag'] == 1){
$list[$i]['upflag'] = '已开启';
}else{
$list[$i]['upflag'] = '已关闭';
}
$objPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$list[$i]['id']);//ID
$objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$list[$i]['groupname']);//标签码
$objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$list[$i]['descript']);//防伪码
$objPHPExcel->getActiveSheet()->setCellValue('D'.($i+2),$list[$i]['upflag']);//防伪码
}
//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;
}