下载phpexcel,将phpexcel导入到框架中
在GitHub中下载PHPExcel源代码到本地。https://github.com/PHPOffice/PHPExcel.git
复制PHPExcel\Classes里的文件到thinkphp框架目录:ThinkPHP\Library\Org\Util,将文件PHPExcel.php修改为PHPExcel.class.php,thinkphp框架格式,方便框架引用。如图:
/** * @param $data 数据,多维数组模式。第一行为标题 $data=array( ['日期','展示量','点击量','价格','ecpm'], ['2018-06-30','10','1','1','100'], ['2018-07-01','20','2','5','500'], ['2018-07-02','1','1','1','100'], ['2018-07-03','3','1','1','100'], ); * @param $filename 文件名 * @param $SheetTitle sheet名称 */ public function excel_export($data,$filename="excelfile",$SheetTitle='Sheet1'){ if(empty($data)){ exit("数据为空,导出错误"); } /*设置文件信息*/ import("ORG.Util.PHPExcel",'','.class.php'); import("ORG.Util.PHPExcel.Reader.Excel2007",'','.php'); $filename=str_replace('.xls', '', $filename).'.xls'; $phpexcel = new \PHPExcel(); $phpexcel->getProperties() ->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); $phpexcel->getActiveSheet()->fromArray($data); $phpexcel->getActiveSheet()->setTitle($SheetTitle); $phpexcel->getActiveSheet()->getStyle("A1:Z1")->getFont()->setName("微软雅黑")->setSize(10)->setBold(true);//设置单元格范围的字体、字体大小、加粗 //$phpexcel->getActiveSheet()->getStyle("A1:Z1")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('#00FF00'); $phpexcel->setActiveSheetIndex(0); header('Content-Type: application/vnd.ms-excel'); header("Content-Disposition: attachment;filename=$filename"); header('Cache-Control: max-age=0'); header('Cache-Control: max-age=1'); header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 $objwriter = \PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5'); $objwriter->save('php://output'); exit; }
传入正确参数,导出excel格式如下图:
图中数据仅为示例,数据未做严谨匹配
如果需要导出的数据只是部分字段,也可以自己设置表头和要导出的字段,代码如下:
//根据excel坐标,添加数据 $phpexcel->setActiveSheetIndex(0) ->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', '公开状态') ->setCellValue('N1', '点击数') ->setCellValue('O1', '评论数') ->setCellValue('P1', '满意度') ->setCellValue('Q1', '满意度理由'); $i=2; foreach ($asks as $ask){ $phpexcel->setActiveSheetIndex(0) ->setCellValue('A'.$i, $ask['id']) ->setCellValue('B'.$i, $ask['atypename']) ->setCellValue('C'.$i, $ask['title']) ->setCellValue('D'.$i, $ask['realname']) ->setCellValue('E'.$i, $ask['mobile']) ->setCellValue('F'.$i, $ask['content']) ->setCellValue('G'.$i, $ask['areaname']) ->setCellValue('H'.$i, $ask['departname']) ->setCellValue('I'.$i, $ask['sdepartname']) ->setCellValue('J'.$i, $ask['statusname']) ->setCellValue('K'.$i, $ask['addtime']) ->setCellValue('L'.$i, $ask['lightname']) ->setCellValue('M'.$i, $ask['openname']) ->setCellValue('N'.$i, $ask['hits']) ->setCellValue('O'.$i, $ask['commentnum']) ->setCellValue('P'.$i, $ask['evalname']) ->setCellValue('Q'.$i, $ask['evalremark']); $i++; }
最新回复 (0)