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,文件相关内容在此文下 ...
随机推荐
- Git学习 -- 远程仓库
第1步:创建SSH Key 在用户目录下的.ssh目录中,查看是否有id_rsa和id_rsa.pub两个文件,若没有则通过以下命令生成秘钥对: $ ssh-keygen -t rsa 第2步:登陆G ...
- .NET中公共变量与属性的区别
在我们的程序中经常会出现以下的代码: 如: 成员变量 public string Name; 或者用属性 private string name ...
- Mysql命令-求一列字段的总和
求和命令 mysql> select SUM(price) from order where create_time>'2016-03-12';+------------+| SUM(pr ...
- SD卡的SPI模式的初始化顺序(转)
为了使SD卡初始化进入SPI模式,我们需要使用的命令有3个:CMD0,ACMD41,CMD55(使用ACMD类的指令前应先发CMD55,CMD55起到一个切换到ACMD类命令的作用). 为什么在使用C ...
- VI中的批量替换 (转载)
1) 文件内全部替换: :%s#abc#123#g (如文件内有#,可用/替换,:%s/abc/123/g) --注:把abc替换成123 (或者: %s/str1/str2/g 用s ...
- mkconfig文件解析
#!/bin/sh -e #mkconfig 100ask24x0 arm arm920t 100ask24x0 Null s3c24x0#s0 s1 ...
- CodeForces 158DIce Sculptures(枚举)
一个暴力的枚举,枚举组成正多边形需要对应覆盖原先的几条边,范围为(1,n/3),然后维护最大值就可以了,注意初始化为-inf. #include<stdio.h> #include< ...
- USACO Section 1.1 Friday the Thirteenth 解题报告
题目 题目描述 黑色星期五是否真的是一件不同寻常的事情?按理来说每个月的13号可能是星期一,或者是星期二...或者是星期天,但是黑色星期五的存在让我们不禁开始猜想,难道每个月的13号刚好是星期五的频率 ...
- 更少的直接百度,更多的取看API
很多时候我们会对于一个jar包中的一个类的某个方法犯迷糊 我们不知道传进去什么样子的参数,这个方法的返回值到底是什么样的. 更多的时候我们不知道这个jar中有没有我们想要的这个方法. 很多时候以前我都 ...
- Android网络开发之OkHttp--基本用法GET
1.OkHttp框架使用了OkIo.jar包,不要忘记添加. 2.对于Request对象是如何实例化的,大家可以参考--java builder设计模式 http://www.cnblogs.com/ ...