博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP 导出 Excell
阅读量:5227 次
发布时间:2019-06-14

本文共 5151 字,大约阅读时间需要 17 分钟。

Vendor('PHPExcel179.PHPExcel'); $objPHPExcel = new PHPExcel(); //创建PHPExcel对象 //设置属性 $objPHPExcel->getProperties()->setCreator("StudyIM")->setLastModifiedBy("StudyIM")->setTitle("StudyIM.com")->setDescription("StudyIM.com")->setKeywords("StudyIM"); //设置宽度 $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(18); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(35); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12); $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(10); $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(20); //设置行高 $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30); $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20); //设置字体样式 $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10); //默认字体大小 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16)->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('A2:N2')->getFont()->setBold(true); //粗体 //合并excel $objPHPExcel->getActiveSheet()->mergeCells('A1:N1'); //设置垂直、水平居中 $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()    ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A2:N2')->getAlignment()    ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //设置边框 $objPHPExcel->getActiveSheet()->getStyle('A2:N2')->getBorders()->getAllBorders()    ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); //前两行单元格内容 $objPHPExcel->setActiveSheetIndex(0)    ->setCellValue('A1',I('module_name').'表查询记录汇总表')    ->setCellValue('A2','ID')    ->setCellValue('B2','用户名')    ->setCellValue('C2','密码')    ->setCellValue('D2','邮箱')    ->setCellValue('E2','性别')    ->setCellValue('F2','用户资金')    ->setCellValue('G2','注册时间')    ->setCellValue('H2','注册IP')    ->setCellValue('I2','最后登录时间')    ->setCellValue('J2','最后登录IP')    ->setCellValue('K2','QQ')    ->setCellValue('L2','手机号')    ->setCellValue('M2','状态')    ->setCellValue('N2','备注'); //数据行设置 for($i = 0;$i < count($modellist);$i++) {
$objPHPExcel->getActiveSheet()->setCellValue('A' . ($i+3), $modellist[$i]['id']); $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i+3), $modellist[$i]['username']); $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i+3), $modellist[$i]['password']); $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i+3), $modellist[$i]['email']); $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i+3), $modellist[$i]['sex']=1?'男':'女'); $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i+3), $modellist[$i]['user_money']); $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i+3), $modellist[$i]['reg_time']); $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i+3), $modellist[$i]['reg_ip']); $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i+3), $modellist[$i]['last_login']?date('Y-m-d H:i:s',$modellist[$i]['last_login']):''); $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i+3), $modellist[$i]['last_ip']); $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i+3), $modellist[$i]['qq']); $objPHPExcel->getActiveSheet()->setCellValue('L' . ($i+3), $modellist[$i]['mobile']); $objPHPExcel->getActiveSheet()->setCellValue('M' . ($i+3), $modellist[$i]['status']=1?'正常':'禁止'); $objPHPExcel->getActiveSheet()->setCellValue('N' . ($i+3), $modellist[$i]['remark']); //设置垂直、水平居中 $objPHPExcel->getActiveSheet()->getStyle('A' . ($i+3).':N'.($i+3))->getAlignment() ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER) ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getRowDimension($i+3)->setRowHeight(16);//行高 //设置边框 $objPHPExcel->getActiveSheet()->getStyle('A' . ($i+3).':N'.($i+3))->getBorders()->getAllBorders() ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); } //sheet命名 $objPHPExcel->getActiveSheet()->setTitle(I('module_name').'表'); //默认打开的sheet $objPHPExcel->setActiveSheetIndex(0); //excel头参数 header("Content-Type:application/vnd.ms-execl"); header('Content-Disposition:attachment;filename='.I('module_name').'表查询结果('.date('YmdHis').').xls');//日期文件名后缀 header('Cache-Control:max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //excel2007为xlsx $objWriter->save('php://output');

转载于:https://www.cnblogs.com/qingxiaoping/p/6057022.html

你可能感兴趣的文章
[OpenGL学习] 缓冲区
查看>>
LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
查看>>
Android架构初探
查看>>
【一头扎进JMS】(2)----ActiviteMQ点对点消息实现
查看>>
Bugku练习
查看>>
渗透测试专用浏览器 - Sandcat
查看>>
Feathers TextInput使KeyboardEvent失效
查看>>
Spring 框架获取 datasource对象的方法
查看>>
没有预热,这不叫高并发,叫并发高
查看>>
Virtual DOM 系列三:Diff算法
查看>>
数据结构之栈与队列
查看>>
时间与时间戳互换
查看>>
rhel7.6上安装Oracle 19.2.0.0 RAC
查看>>
10个关于Android开发的实用教程
查看>>
数据库事务的四大特性以及事务的隔离级别
查看>>
软件研发网站收集
查看>>
C# 中HttpClient的使用中同步异步问题
查看>>
改动Android启动画面
查看>>
1002.写出这个数
查看>>
[BZOJ1316]树上的询问 点分治
查看>>