PHPEXCEL实例-导出EXCEL
PHPExcel 是相当强大的 MS Office Excel 文档生成类库,当需要输出比较复杂格式数据的时候,PHPExcel 是个不错的选择。
- <?php
- /*
- * 导出EXCEL
- * 程序:刘兴
- * 时间:2010-6
- */
- ?>
- <?php
- require_once("connDB.php");//引入数据库连接参数
- require_once("PExcel/PHPExcel.php");
- ?>
- <?php
- $year=$_POST[year];
- $jd=$_POST[jd];
- set_time_limit(900);
- /** Error reporting */
- error_reporting(E_ALL);
- /** PHPExcel */
- $sqlxm="select count(*) as cxm from khxm where lb=1 and isyx=0";
- $resultxm=mysql_query($sqlxm) or die("SQL语句执行错误!");
- $rowxm = mysql_fetch_array($resultxm);
- $c1=$rowxm['cxm'];
- $sqlxm="select count(*) as cxm from khxm where lb=2 and isyx=0";
- $resultxm=mysql_query($sqlxm) or die("SQL语句执行错误!");
- $rowxm = mysql_fetch_array($resultxm);
- $c2=$rowxm['cxm'];
- $sqlxm="select count(*) as cxm from khxm where lb=3 and isyx=0";
- $resultxm=mysql_query($sqlxm) or die("SQL语句执行错误!");
- $rowxm = mysql_fetch_array($resultxm);
- $c3=$rowxm['cxm'];
- // Create new PHPExcel object
- $objPHPExcel = new PHPExcel();
- // Set properties
- $objPHPExcel->getProperties()->setCreator("gxds");
- $objPHPExcel->getProperties()->setLastModifiedBy("gxds");
- $objPHPExcel->getProperties()->setTitle("gxdskhtj");
- $objPHPExcel->getProperties()->setSubject("gxdskhtj");
- $objPHPExcel->getProperties()->setDescription("gxdskhtj, gxds.");
- $objPHPExcel->getProperties()->setKeywords("gxdskhtj");
- $objPHPExcel->getProperties()->setCategory("gxtj result file");
- // Add some data
- $noshow = array("year", "time", "peopleid", "bm","kbm");
- $objPHPExcel->setActiveSheetIndex(0);
- $objActSheet = $objPHPExcel->getActiveSheet();
- $objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(2, 3);
- $db=new mysqli("localhost","ssss","aaaaa","bbbb");
- mysqli_query($db,"SET NAMES utf8");
- $result=$db->query("call gxtj($year,$jd)");
- $i=4;
- while( $row = $result->fetch_array(MYSQLI_ASSOC))
- {
- $c=0;
- $zt=1;
- $totalfs=100;
- $tfs=0;
- $isstart=false;
- $j=65;
- $asc_cell=chr($j);
- while ($key=key($row)){
- $ex_title=$asc_cell."3";
- $ex_cell=$asc_cell.$i;
- $value=current($row);
- if (in_array($key, $noshow)) {
- next($row);
- continue;
- }
- if (is_numeric($value) or is_null($value) ){
- if (is_null($value)) $value='0';
- if (!$isstart) {
- $isstart=true;
- $startasc=$asc_cell;
- $endasc=$asc_cell;
- }
- }
- if ($isstart){
- $c++;
- if ($zt==1){
- if ($c>$c1) {
- $zt++;
- $ge=$startasc.'2:'.$endasc.'2';
- $objActSheet->mergeCells("$ge");
- $objActSheet->setCellValue($startasc.'2',"岗位职责、效能考核");
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getAlignment()
- ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getAlignment()
- ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getFill()
- ->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getFill()
- ->getStartColor()->setARGB('0DB0E59FF');
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')
- ->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
- $objPHPExcel->getActiveSheet()->getStyle($ex_title)
- ->getFont()->setBold(true);
- $startasc=$asc_cell;
- $endasc=$asc_cell;
- $totalfs-=$tfs;
- $tfs=0;
- $c=1;
- }
- else{
- $endasc=$asc_cell;
- }
- $tfs+=$value;
- if ($tfs>50) {
- $tfs=50;
- }
- }
- if ($zt==2){
- if ($c>$c2) {
- $zt++;
- $ge=$startasc.'2:'.$endasc.'2';
- $objActSheet->mergeCells("$ge");
- $objActSheet->setCellValue($startasc.'2',"考试");
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getAlignment()
- ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getAlignment()
- ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getFill()
- ->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getFill()
- ->getStartColor()->setARGB('0DB0E59FF');
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')
- ->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
- $objPHPExcel->getActiveSheet()->getStyle($ex_title)
- ->getFont()->setBold(true);
- $startasc=$asc_cell;
- $endasc=$asc_cell;
- $totalfs-=$tfs;
- $tfs=0;
- $c=1;
- }
- else{
- $endasc=$asc_cell;
- }
- $tfs+=$value;
- if ($tfs>30) {
- $tfs=30;
- }
- }
- if ($zt==3){
- if ($c=$c3) {
- $zt++;
- $ge=$startasc.'2:'.$endasc.'2';
- $objActSheet->mergeCells("$ge");
- $objActSheet->setCellValue($startasc.'2',"科长考核");
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getAlignment()
- ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getAlignment()
- ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getFill()
- ->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')->getFill()
- ->getStartColor()->setARGB('0DB0E59FF');
- $objPHPExcel->getActiveSheet()->getStyle($startasc.'2')
- ->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
- $objPHPExcel->getActiveSheet()->getStyle($ex_title)
- ->getFont()->setBold(true);
- $startasc=$asc_cell;
- $endasc=$asc_cell;
- $totalfs-=$tfs;
- $tfs=0;
- $c=0;
- }
- else{
- $endasc=$asc_cell;
- }
- $tfs+=$value;
- if ($tfs>20) {
- $tfs=20;
- }
- }
- }
- $objActSheet->setCellValue($ex_title,$key);
- $objActSheet->setCellValue($ex_cell,$value);
- //设置单元格宽度
- $objActSheet->getColumnDimension($asc_cell)->setWidth(16);
- $objPHPExcel->getActiveSheet()->getStyle($ex_title)->getAlignment()
- ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle($ex_title)->getAlignment()
- ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle($ex_cell)->getAlignment()
- ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle($ex_cell)->getAlignment()
- ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
- //设置填充格式及字体颜色
- $objPHPExcel->getActiveSheet()->getStyle($ex_cell)
- ->getNumberFormat()->setFormatCode('#,##0.00');
- $objPHPExcel->getActiveSheet()->getStyle($ex_title)->getFill()
- ->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
- $objPHPExcel->getActiveSheet()->getStyle($ex_title)->getFill()
- ->getStartColor()->setARGB('0DB0E50A1');
- $objPHPExcel->getActiveSheet()->getStyle($ex_title)
- ->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
- //设置字体粗体
- $objPHPExcel->getActiveSheet()->getStyle($ex_title)
- ->getFont()->setBold(true);
- $j++;
- if ($j>90) {
- $j=65;
- $asc_cell="AA" ;
- }
- elseif (strlen($asc_cell)>1) {
- $asc_cell=substr($asc_cell,0,strlen($asc_cell)-1).chr($j);
- }
- else{
- $asc_cell=chr($j);
- }
- next($row);
- }
- $ge=$asc_cell.'2:'.$asc_cell.'3';
- $objActSheet->mergeCells("$ge");
- $objPHPExcel->getActiveSheet()->getStyle($asc_cell.'2')->getAlignment()
- ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle($asc_cell.'2')->getAlignment()
- ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objActSheet->setCellValue($asc_cell.'2',"总分");
- $objActSheet->setCellValue($ex_cell,$totalfs);
- //设置填充格式及字体颜色
- $objPHPExcel->getActiveSheet()->getStyle($ex_cell)
- ->getNumberFormat()->setFormatCode('#,##0.00');
- $objPHPExcel->getActiveSheet()->getStyle($asc_cell.'2')->getFill()
- ->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
- $objPHPExcel->getActiveSheet()->getStyle($asc_cell.'2')->getFill()
- ->getStartColor()->setARGB('0DB0E59FF');
- //设置字体颜色
- $objPHPExcel->getActiveSheet()->getStyle($asc_cell.'2')
- ->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
- $objPHPExcel->getActiveSheet()->getStyle($asc_cell.'2')
- ->getFont()->setBold(true);
- $i++;
- }
- //标题栏合并单元格,并设置居中
- $objActSheet->setCellValue('A1',$year."年".$jd."季度绩效考核情况统计表");
- $value=$objPHPExcel->getActiveSheet()->getCell('A1')->getValue();;
- $ge='A1:'.$asc_cell.'1';
- $objActSheet->mergeCells("$ge");
- $objActSheet->setCellValue('A1',$value);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()
- ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()
- ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('A1')
- ->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
- $objPHPExcel->getActiveSheet()->getStyle('A1')
- ->getFont()->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('A1')
- ->getFont()->setSize(26);
- //姓名栏合并单元格,并设置居中
- $value=$objPHPExcel->getActiveSheet()->getCell('A3')->getValue();;
- $ge='A2:A3';
- $objActSheet->mergeCells("$ge");
- $objActSheet->setCellValue('A2',$value);
- $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()
- ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()
- ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('A2')->getFill()
- ->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
- $objPHPExcel->getActiveSheet()->getStyle('A2')->getFill()
- ->getStartColor()->setARGB('0DB0E59FF');
- $objPHPExcel->getActiveSheet()->getStyle('A2')
- ->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
- $objPHPExcel->getActiveSheet()->getStyle('A2')
- ->getFont()->setBold(true);
- //科室栏合并单元格,并设置居中
- $value=$objPHPExcel->getActiveSheet()->getCell('B3')->getValue();;
- $ge='B2:B3';
- $objActSheet->mergeCells("$ge");
- $objActSheet->setCellValue('B2',$value);
- $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()
- ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()
- ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('B2')->getFill()
- ->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
- $objPHPExcel->getActiveSheet()->getStyle('B2')->getFill()
- ->getStartColor()->setARGB('0DB0E59FF');
- $objPHPExcel->getActiveSheet()->getStyle('B2')
- ->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
- $objPHPExcel->getActiveSheet()->getStyle('B2')
- ->getFont()->setBold(true);
- //单元格边框及颜色
- $objPHPExcel->getActiveSheet()->getStyle('A2:'.$ex_cell)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- $objPHPExcel->getActiveSheet()->getStyle('A2:'.$ex_cell)->getBorders()->getAllBorders()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
- // sheet名称
- $objPHPExcel->getActiveSheet()->setTitle('绩效考核统计');
- // Set active sheet index to the first sheet, so Excel opens this as the first sheet
- $objPHPExcel->setActiveSheetIndex(0);
- // Redirect output to a clients web browser (Excel5)通知下载
- $fn="gxtj-$year-$jd.xls";
- header('Content-Type: application/vnd.ms-excel; charset=utf-8');
- header("Content-Disposition: attachment;filename=$fn");
- header('Cache-Control: max-age=0');
- $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- $objWriter->save('php://output');
- exit;
- ?>
PHPEXCEL实例-导出EXCEL的更多相关文章
- 使用PHPExcel导入导出excel格式文件
使用PHPExcel导入导出excel格式文件 作者:zccst 因为导出使用较多,以下是导出实现过程. 第一步,将PHPExcel的源码拷贝到项目的lib下 文件包含:PHPExcel.ph ...
- tp5.0 composer下载phpexcel 实现导出excel 表格
tp5.0 composer下载phpexcel 实现导出excel 表格 1.在下载好的tp5.0 框架里面使用 composer 下载phpexcel 的插件 composer require p ...
- tp5使用PHPexcel扩展导出excel表
1,使用composer安装phpexcel包: composer require phpoffice/phpexcel 2,在控制器中创建方法: (1)使用PHPexcel扩展.代码如下 /** * ...
- phpcms 用phpexcel导入导出excel
html <form method="post" action="?m=content&c=content&a=public_add_excel&q ...
- phpexcel 导入导出excel表格
phpexcel中文实用手册 转载:http://www.cnblogs.com/freespider/p/3284828.html 下面是总结的几个使用方法 include 'PHPExcel.ph ...
- 利用PHPExcel快速导出excel
<?php $objPHPExcel = new PHPExcel(); $objSheet = $objPHPExcel->getActiveSheet(); $array = arra ...
- PHP导入导出excel表格图片(转)
写excel的时候,我用过pear的库,也用过pack压包的头,同样那些利用smarty等作的简单替换xml的也用过,csv的就更不用谈了.呵呵.(COM方式不讲了,这种可读的太多了,我也写过利用wp ...
- PHP导入导出excel表格图片的代码和方法大全
基本上导出的文件分为两种: 1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已.修改这种文件后再保存,通常会提示你是否要转换成Excel文件 ...
- php两种导出excel的方法
所需要的:jquery库,phpexcel插件,页面导出excel效果测试文件explode.php,excel导出功能实现文件exp.php和explode_excel.php,文件相关内容在此文下 ...
随机推荐
- CodeForces 678D Iterated Linear Function
简单矩阵快速幂. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm& ...
- 介绍Python程序员常用的IDE和其它开发工具
概述 “工欲善其事,必先利其器”,如果说编程是程序员的手艺,那么IDE就是程序员的吃饭家伙了. IDE 的全称是Integration Development Environment(集成开发环境), ...
- MVC 使用Jquery的$.post传递参数
MVC中,如果要使用 $.post 给 COntroller 传递参数,需要类实现 属性 get set,这样才行
- 博弈论最简单例子TacTicToe
博弈论是人工智能中的一个分支.顾名思义就是下棋的算法.当然引申出来的应用可能不止用来下棋,也可以用来做游戏或者模拟战争策略等. 博弈的基本算法也是模拟人的思维,比如当自己下子时遍历所有可能寻求最有利步 ...
- 用MyEclipse自动生成hibernate映射文件和实体类
创建数据库,创建相应的表 点击图标,选择MyEclipse Datebase Explorer 右击空白区域,选择new菜单,根据提示创建数据库连接,创建好后会显示你所创建的连接名,如图mysqldb ...
- opencv ,亮度调整【【OpenCV入门教程之六】 创建Trackbar & 图像对比度、亮度值调整
http://blog.csdn.net/poem_qianmo/article/details/21479533 [OpenCV入门教程之六] 创建Trackbar & 图像对比度.亮度值调 ...
- 为什么MVC不是一种设计模式? ---比较Backbone和Ext4.x在MVC实现上的差异
为什么MVC不是一种设计模式? ---比较Backbone和Ext4.x在MVC实现上的差异 大漠穷秋 前言 圣人云:不想做妈咪的小姐不是好码农. 每一个码农的心中都有一个终极理想,那就是有一天不用再 ...
- 安装apk时出现错误Failure [INSTALL_FAILED_DEXOPT]问题解决的方法
在android4.0源码里面编译出来apk后,用adb install (或adb install -r 重装)安装时,报错[INSTALL_FAILED_DEXOPT]. xu@xu-PC:~$ ...
- 用TcpClient如何获取远程网页的内容
用TcpClient如何获取远程网页的内容 private string GetHTMLTCP(string URL) { string strHTML = "";//用来保存获得 ...
- MJExtention
+ (NSDictionary *)mj_objectClassInArray { // key : 属性名 // value : 类名 return @{ @"dogs" : @ ...