ThinkPHP6如何使用phpexcel导出excel并设置样式
public function save_import(){ $excleFile = input('excel'); //读取execel文件 $path = public_path().$excleFile; //实例化PHPExcel类 $objPHPExcel = new \PHPExcel(); $objPHPExcel->createSheet(); //创建新的内置表 $objPHPExcel->setActiveSheetIndex(); //吧新创建的sheet设定为当前活动sheet $objPHPExcel->getActiveSheet()->freezePane('A2');//锁定这个单元格,则该单元格所有的左边区域和上方区域都会被锁定,也就是不受滚动条的影响,像浮在那一样,这就是锁定单元格的原理。所以锁定A2就是冻结第一行了 //设置背景颜色 $objPHPExcel->getActiveSheet()->getStyle('A1:M1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('D9FFC125'); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20); $objSheet=$objPHPExcel->getActiveSheet(); //获取当前活动sheet $data=Integrals::order('id', 'asc')->select()->toArray(); $objSheet->setTitle("积分兑换");//给当前活动sheet设置名称 $objSheet->setCellValue('A1','ID')->setCellValue('B1','卡号')->setCellValue('C1','姓名')->setCellValue('D1','累计积分')->setCellValue('E1','已用积分'); $j=2; foreach($data as $key=>$val){ $objSheet->setCellValue('A'.$j,$val['id'])->setCellValue('B'.$j,$val['card'])->setCellValue('C'.$j,$val['name'])->setCellValue('D'.$j,$val['integral'])->setCellValue('E'.$j,$val['used_integral']); $j++; } //设置保存的Excel表格名称 $filename ="积分兑换 - ".date("Y-m-d").'.xls'; //设置浏览器窗口下载表格 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; }
文章评论(0)