效果图如下:

代码如下:代码注释不全,请大家参考phpexcel中文帮助手册

<?php
require_once 'PHPExcel.php';
function bfb($n)
{
return $n*100 . '%';
}
$objPHPExcel = new PHPExcel();// 创建一个处理对象实例
$objPHPExcel->createSheet();//创建sheet
$objPHPExcel->setActiveSheetIndex(0);
$objActSheet = $objPHPExcel->getActiveSheet();
$column_number = 6;
//设置表格标题
$objActSheet->setCellValue('A1', '2016级一班 学科学生成绩记载表');
//设置字体大小
$objActSheet->getStyle('A1')->getFont()->setName('宋体'); $objActSheet->getStyle('A1')->getFont()->setSize(40); $objActSheet->getStyle('A1')->getFont()->setBold(true); //合并标题单元格 $objActSheet->getStyle('B2')->getAlignment()->applyFromArray(
array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true )
);
$styleArray1 = array(
'font' => array(
'bold' => false,
'size'=>12,
'color'=>array(
'argb' => '00000000',
),
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
);
// 将A1单元格设置为加粗,居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray1);
$objActSheet->mergeCells( 'A1:'.chr(ord('A')+$column_number-1).'1');//合并标题
$objPHPExcel->getActiveSheet()->mergeCells('A2:A3');
$objActSheet->setCellValue('A2', '姓名');
$objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->mergeCells('B2:C2');
$objActSheet->setCellValue('B2', '成绩');
$objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->setCellValue('B3', '分数');
$objActSheet->setCellValue('C3', '等级');
$objPHPExcel->getActiveSheet()->getStyle('B3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('B3')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('C3')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
###########################
$objPHPExcel->getActiveSheet()->mergeCells('D2:D3');
$objActSheet->setCellValue('D2', '姓名');
$objPHPExcel->getActiveSheet()->getStyle('D2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('D2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->mergeCells('E2:F2');
$objActSheet->setCellValue('E2', '成绩');
$objPHPExcel->getActiveSheet()->getStyle('E2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('E2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->setCellValue('E3', '分数');
$objActSheet->setCellValue('F3', '等级');
$objPHPExcel->getActiveSheet()->getStyle('E3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('E3')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('F3')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
#########################
$styleThinBlackBorderOutline = array(
'borders' => array (
'outline' => array (
'style' => PHPExcel_Style_Border::BORDER_THIN, //设置border样式
//'style' => PHPExcel_Style_Border::BORDER_THICK, 另一种样式
'color' => array ('argb' => 'FF000000'), //设置border颜色
),
),
);
$lie = array('A','B','C','D','E','F');
foreach($lie as $l){
$objPHPExcel->getActiveSheet()->getColumnDimension($l)->setWidth(15);
}
$data1 = array(
array('name'=>'学生姓名1','code'=>90,'dj'=>'优'),
array('name'=>'学生姓名2','code'=>91,'dj'=>'优'),
array('name'=>'学生姓名3','code'=>92,'dj'=>'优'),
array('name'=>'学生姓名4','code'=>93,'dj'=>'优'),
array('name'=>'学生姓名5','code'=>94,'dj'=>'优'),
array('name'=>'学生姓名6','code'=>95,'dj'=>'优'),
array('name'=>'学生姓名7','code'=>96,'dj'=>'优')
);
$data2 = array(
array('name'=>'学生姓名1','code'=>90,'dj'=>'优'),
array('name'=>'学生姓名2','code'=>91,'dj'=>'优'),
array('name'=>'学生姓名3','code'=>92,'dj'=>'优'),
array('name'=>'学生姓名4','code'=>93,'dj'=>'优'),
array('name'=>'学生姓名5','code'=>94,'dj'=>'优'),
array('name'=>'学生姓名6','code'=>95,'dj'=>'优'),
array('name'=>'学生姓名7','code'=>96,'dj'=>'优')
);
$j = $i = 4;
$code = 0;
foreach($data1 as $k=>$v){
$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $v['name']);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $v['code']);
$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $v['dj']);
$objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(30);
$code += $v['code'];
$i++;
}
foreach($data2 as $k=>$v){
$objPHPExcel->getActiveSheet()->setCellValue('D' . $j, $v['name']);
$objPHPExcel->getActiveSheet()->setCellValue('E' . $j, $v['code']);
$objPHPExcel->getActiveSheet()->setCellValue('F' . $j, $v['dj']);
$objPHPExcel->getActiveSheet()->getStyle('D'.$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('D'.$j)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E'.$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('E'.$j)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F'.$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('F'.$j)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getRowDimension($j)->setRowHeight(30);
$code += $v['code'];
$j++;
}
$count = count($data1)+count($data2);
$pjf = $code / $count ;
$hgl = bfb(1);
$ysl = bfb(1);
if($i != $j){
die('导出成绩表出错');
}
$endindex1 = $i;
$endindex2 = $i+1;
$endindex3 = $i+2;
$objPHPExcel->getActiveSheet()->setCellValue('A' . $endindex1, '总人数');
$objPHPExcel->getActiveSheet()->getStyle('A'.$endindex1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A'.$endindex1)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $endindex1, $count);
$objPHPExcel->getActiveSheet()->getStyle('B'.$endindex1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('B'.$endindex1)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('C' . $endindex1, '合格人数');
$objPHPExcel->getActiveSheet()->getStyle('C'.$endindex1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('C'.$endindex1)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('D' . $endindex1, $count);
$objPHPExcel->getActiveSheet()->getStyle('D'.$endindex1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('D'.$endindex1)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('E' . $endindex1, '优生人数');
$objPHPExcel->getActiveSheet()->getStyle('E'.$endindex1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('E'.$endindex1)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('F' . $endindex1, $count);
$objPHPExcel->getActiveSheet()->getStyle('F'.$endindex1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('F'.$endindex1)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('A' . $endindex2, '平均分');
$objPHPExcel->getActiveSheet()->getStyle('A'.$endindex2)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A'.$endindex2)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $endindex2, $pjf);
$objPHPExcel->getActiveSheet()->getStyle('B'.$endindex2)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('B'.$endindex2)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('C' . $endindex2, '合格率');
$objPHPExcel->getActiveSheet()->getStyle('C'.$endindex2)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('C'.$endindex2)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('D' . $endindex2, $hgl);
$objPHPExcel->getActiveSheet()->getStyle('D'.$endindex2)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('D'.$endindex2)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('E' . $endindex2, '优生率');
$objPHPExcel->getActiveSheet()->getStyle('E'.$endindex2)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('E'.$endindex2)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('F' . $endindex2, $ysl);
$objPHPExcel->getActiveSheet()->getStyle('F'.$endindex2)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('F'.$endindex2)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//登分人签名
$writename = '杨捷成';
$objPHPExcel->getActiveSheet()->setCellValue('E' . $endindex3, '登分人签名');
$objPHPExcel->getActiveSheet()->getStyle('E'.$endindex3)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('E'.$endindex3)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue('F' . $endindex3, $writename);
$objPHPExcel->getActiveSheet()->getStyle('F'.$endindex3)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('F'.$endindex3)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F'.$endindex3)->getFill()->getStartColor()->setARGB('FF808080'); // 将背景设置为浅粉色
//粗体下划线有背景色
$objPHPExcel->getActiveSheet()->getStyle('F'.$endindex3)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE); //***********************画出单元格边框*****************************
$styleArray = array(
'borders' => array(
'allborders' => array(
//'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框
//'color' => array('argb' => 'FFFF0000'),
),
),
);
$objPHPExcel->getActiveSheet()->getStyle('A1:F'.$endindex2)->applyFromArray($styleArray);//这里就是画出从单元格A5到N5的边框,看单元格最右边在哪哪个格就把这个N改为那个字母替代
//***********************画出单元格边框结束*****************************
$path = 'title.xlsx';
// 生成2007excel格式的xlsx文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setOffice2003Compatibility(true);
//echo $i;
$objWriter->save($path); ?>

代码存在冗余,后面会做封装!O(∩_∩)O哈哈~

phpexcel导出成绩表的更多相关文章

  1. 使用PHPExcel导出数据库表结构及内容

    导出表结构: mysql> desc user ; +----------+--------------+------+-----+---------------------+--------- ...

  2. PHPExcel导出工作蒲(多表合并)教程+详细代码解读

    最近做了一个需求,导出统计数据,因为需要同时导出多个不同的统计数据,所以不能像以往导出数据列表一样去实现这个需求,刚好空下来就记录一下(PHPExcel导出Excel多sheet合并) 一.主要使用的 ...

  3. thinkphp导入导出excel表单数据

    在PHP项目经常要导入导出Excel表单. 先去下载PHPExcel类库文件,放到相应位置. 我在thinkphp框架中的位置为ThinkPHP/Library/Org/Util/ 导入 在页面上传e ...

  4. JS-纯js制作动态成绩表(流程控制语句+js内置对象)

    流程控制for循环+if判断+Math对象+Array对象+Date对象制作成绩表 <!DOCTYPE html><html> <head> <meta ch ...

  5. JS-结合html综合练习js的对象——班级成绩表制作

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>对 ...

  6. phpExcel导出excel加超级链接的实例代码[转]

    phpexcel实现的导出excel文件的代码,且可以在excel文件中加入超级链接. 说明:PHPExcel的开发包Tests目录有详细使用实例.以下代码支持中文,注意文件编码,文件保存为utf-8 ...

  7. tp phpexcel 导出后台访问方法

    public function phpexcel(){ //-----excel表导出 $dataid = $_GET['dataid']; $status = $_GET['status']; $F ...

  8. sql面试题(学生表_课程表_成绩表_教师表)

    原帖链接:http://bbs.csdn.net/topics/280002741 表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程 ...

  9. C/C++编译和链接过程详解 (重定向表,导出符号表,未解决符号表)

    详解link  有 些人写C/C++(以下假定为C++)程序,对unresolved external link或者duplicated external simbol的错误信息不知所措(因为这样的错 ...

随机推荐

  1. redis在spring-boot中的应用

    Redis(REmote DIctionary Server) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用ANSI C语言编写.遵守BS ...

  2. JSP7(Cookie与javamail)

    一.cookie是什么意思? 英文直接翻译过来的意思呢就是小甜品 Cookie英文意指饼干,不过在电脑术语中它可不像饼干那么简单.简单的说,Cookie就是服务器暂存放在你计算机上的一笔资料,好让服务 ...

  3. 用vue2.x注册一个全局的弹窗alert组件

    一.在实际的开发当中,弹窗是少不了的,默认系统的弹窗样式太丑,难以满足项目的实际需求,所以需要自己定义弹窗组件,把弹窗组价定义为全局的,这样减少每次使用的时候引入麻烦,节省开发时间.本文将分享如何定义 ...

  4. php编译安装php-5.6

    #php编译安装php-5.6 ,Nginx+php使用 #!/bin/sh #php编译安装php-5.6 ,Nginx+php使用 #定义函数,默认绿色输出 '#' 开头为红色 function ...

  5. 洛谷银牛派对SPFA

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  6. maven项目对于测试时“无法加载主类”的解决方案

    1.右键maven项目,选择  build path --------->Configure Build Path  ,执行下列操作,保存即可.

  7. Spring任务调度之SpringTask基于XML和基于注解的使用示例

    使用Spring的环境要求是:JDK1.8以上.Maven3.0以上. Maven依赖 SpringTask集成在SpringContext中,所以只需要SpringContext即可. 可以使用ma ...

  8. underscore源码解析(一)

    留存root // Establish the root object, `window` (`self`) in the browser, `global` // on the server, or ...

  9. java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@412d7230

    近期遇到了如标题这种错误,再次记录解决方法.本文參考帖子: http://bbs.csdn.net/topics/390196217 出现此bug的原因是在内存回收上.里面用Bitamp的代码为: t ...

  10. android checkbox 未选中状态 已选中状态 替换成自己的图片

    效果图: 未选中状态: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...