PHPExcel 生成excel统计表格

db.php为数据库操作类, $config为数据库配置,PHPExcel版本为PHPExcel_1.8.0,  php代码:

$dir = dirname(__FILE__);
require $dir . "/PHPExcel/db.php";
require $dir . "/PHPExcel/PHPExcel.php";
$object = new PHPExcel();
$db = new db($config); $objSheet = $object->getActiveSheet(); //文字居中和文字设置
$objSheet->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objSheet->getDefaultStyle()->getFont()->setName("微软雅黑")->setSize(14);
$objSheet->getStyle("A2:X2")->getFont()->setSize(20)->setBold(TRUE);
$objSheet->getStyle("A3:X3")->getFont()->setSize(16)->setBold(TRUE); //设置行高
$objSheet->getRowDimension(2)->setRowHeight(35);
$objSheet->getRowDimension(3)->setRowHeight(25);
$objSheet->getRowDimension(4)->setRowHeight(20); //填充颜色
$objSheet->getStyle("A2:X2")->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('ffff00');
$objSheet->getStyle("A3:X3")->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('ff0000'); //sheet名长
$objSheet->setTitle("成绩表"); //查询所有的年级
$i = 0;
$data_g = $db->getAllGrade();
foreach($data_g as $g_k=>$g_v) { //查询每个年级有几个班
$data_c = $db->getAllClassBYGrade($g_v['grade']); //获取年级所在单元格并填充单元格
$gradeIndex = $i*2;
$grade = getCells($gradeIndex);
$objSheet->setCellValue($grade."2", $g_v['grade']."年级"); foreach($data_c as $c_v) { //查询所有学生成绩
$data = $db->getDataByClass($c_v['class'], $g_v['grade']); //获取姓名和分数所在单元格
$nameIndex = $i*2;
$scoreIndex = $i*2+1;
$index_c = getCells($nameIndex);
$score = getCells($scoreIndex); //填充班级行
$objSheet->setCellValue($index_c."3", $c_v['class']."班"); //填充栏目名称
$objSheet->setCellValue($index_c."4", "姓名")->setCellValue($score."4", "分数");
//$objSheet->getStyle($index_c)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
//自动换行
//$objSheet->getStyle($nameIndex)->getAlignment()->setWrapText(true); //开始输出内容
$j = 5;
foreach($data as $v) {
$objSheet->setCellValue($index_c.$j, $v['name'])->setCellValue($score.$j, ' '.$v['score']);
//设置行高
$objSheet->getRowDimension($j)->setRowHeight(20);
$j++;
}
$i++; //合并班级单元格
$endClass = getCells($i*2-1);
$objSheet->mergeCells($index_c."3:".$endClass."3"); //设置班级的边框
$styleArray = borderStyle('0000FF');
$objSheet->getStyle($index_c."3:".$endClass."3")->applyFromArray($styleArray);
} //合并年级单元格
$endGrade = getCells($i*2-1);
$objSheet->mergeCells($grade."2:".$endGrade."2"); //设置年级的边框
$styleArray = borderStyle('00FF00');
$objSheet->getStyle($grade."2:".$endGrade."2")->applyFromArray($styleArray); } //通过传入参数(1-26)得到A-Z的字母列
function getCells($index) {
$str = range(A, Z);
return $str[$index];
} //设置边框样式
function borderStyle($color) {
$styleArray = array(
'borders' => array(
'outline' => array(
'style' => PHPExcel_Style_Border::BORDER_THICK,
'color' => array('rgb' => $color),
),
),
);
return $styleArray;
} header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($object, 'Excel5');
$objWriter->save('php://output');

数据库结构:

PHPExcel 生成excel统计表格

效果图:

PHPExcel 生成excel统计表格

PHPExcel 生成excel统计表格

上一篇:Java的序列化和反序列化


下一篇:JavaWeb学习 (十四)————JSP基础语法