//处理顶部标题 if(!empty($top_title)){ $objPHPExcel->getActiveSheet()->mergeCells('A1:'.$cellKey[count($cellName)-1].'1');//合并单元格(如果要拆分单元格是需要先合并再拆分的,否则程序会报错) $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1',$top_title); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); }else{ $topNumber=1; } //处理表头 foreach ($cellName as $k=>$v) { $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellKey[$k].$topNumber, $v[1]);//设置表头数据 //$objPHPExcel->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//冻结窗口,作用不详,会出现一条竖线 $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//设置是否加粗 $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中 if($v[3] > 0)//大于0表示需要设置宽度 { $objPHPExcel->getActiveSheet()->getColumnDimension($cellKey[$k])->setWidth($v[3]);//设置列宽度 } } //处理数据 foreach ($data as $k=>$v) { foreach ($cellName as $k1=>$v1) { $objPHPExcel->getActiveSheet()->setCellValue($cellKey[$k1].($k+1+$topNumber), $v[$v1[0]]); if($v['end'] > 0) { if($v1[2] == 1)//这里表示合并单元格 { $objPHPExcel->getActiveSheet()->mergeCells($cellKey[$k1].$v['start'].':'.$cellKey[$k1].$v['end']); $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].$v['start'])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); } } if($v1[4] != "" && in_array($v1[4], array("LEFT","CENTER","RIGHT"))) { $v1[4] = eval('return \PHPExcel_Style_Alignment::HORIZONTAL_'.$v1[4].';'); //这里也可以直接传常量定义的值,即left,center,right;小写的strtolower $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].($k+1+$topNumber))->getAlignment()->setHorizontal($v1[4]); } } } //导出execl ob_end_clean();//清除缓冲区,避免乱码 header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"'); header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; }
使用php加phpexcel类库将数据导出到表格
/**
* execl数据导出
* @param string $file_name 用于导出生成文件名的前缀
* @param array $cellName 表头及字段名
* @param array $data 导出的表数据
* @param string $top_title 表格顶部的标题
* phpexcel下载地址:https://pan.baidu.com/s/1e6WXeg3csRCIaSFaqwLKzQ
*
* 特殊处理:合并单元格需要先对数据进行处理
*/
function Export2Excel($file_name,$cellName,$data,$top_title='')
{
// $cellName=[
// ['数据的键名','列名','控制合并单元格','列宽度','内容显示位置'],//前两个必填,后三个选填
// ['id','ID','','100','LEFT'],
// ['name','姓名','','100','LEFT'],
// ['age','年龄','','100','RIGHT'],
// ];
// $data=[
// ['id'=>'1','name'=>'tyong','age'=>25],
// ['id'=>'2','name'=>'tt','age'=>12],
// ];
//引入核心文件
vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
//定义配置
$topNumber = 2;//顶部标题占用1行,所以从第二行开始
$xlsTitle = iconv('utf-8', 'gb2312', $file_name);//文件名称
$fileName = $file_name.date('_YmdHis');//文件名称
$cellKey = array(
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM',
'AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'
);
$objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(30);//所有单元格(列)默认宽度
//写在处理的前面(了解表格基本知识,已测试)
// $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);//所有单元格(行)默认高度
// $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//所有单元格(列)默认宽度
// $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);//设置行高度
// $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);//设置列宽度
// $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);//设置文字大小
// $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);//设置是否加粗
// $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);// 设置文字颜色
// $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置文字居左(HORIZONTAL_LEFT,默认值)中(HORIZONTAL_CENTER)右(HORIZONTAL_RIGHT)
// $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
// $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);//设置填充颜色
// $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF7F24');//设置填充颜色
//处理顶部标题 if(!empty($top_title)){ $objPHPExcel->getActiveSheet()->mergeCells('A1:'.$cellKey[count($cellName)-1].'1');//合并单元格(如果要拆分单元格是需要先合并再拆分的,否则程序会报错) $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1',$top_title); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); }else{ $topNumber=1; } //处理表头 foreach ($cellName as $k=>$v) { $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellKey[$k].$topNumber, $v[1]);//设置表头数据 //$objPHPExcel->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//冻结窗口,作用不详,会出现一条竖线 $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//设置是否加粗 $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中 if($v[3] > 0)//大于0表示需要设置宽度 { $objPHPExcel->getActiveSheet()->getColumnDimension($cellKey[$k])->setWidth($v[3]);//设置列宽度 } } //处理数据 foreach ($data as $k=>$v) { foreach ($cellName as $k1=>$v1) { $objPHPExcel->getActiveSheet()->setCellValue($cellKey[$k1].($k+1+$topNumber), $v[$v1[0]]); if($v['end'] > 0) { if($v1[2] == 1)//这里表示合并单元格 { $objPHPExcel->getActiveSheet()->mergeCells($cellKey[$k1].$v['start'].':'.$cellKey[$k1].$v['end']); $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].$v['start'])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); } } if($v1[4] != "" && in_array($v1[4], array("LEFT","CENTER","RIGHT"))) { $v1[4] = eval('return \PHPExcel_Style_Alignment::HORIZONTAL_'.$v1[4].';'); //这里也可以直接传常量定义的值,即left,center,right;小写的strtolower $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].($k+1+$topNumber))->getAlignment()->setHorizontal($v1[4]); } } } //导出execl ob_end_clean();//清除缓冲区,避免乱码 header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"'); header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; }
//处理顶部标题 if(!empty($top_title)){ $objPHPExcel->getActiveSheet()->mergeCells('A1:'.$cellKey[count($cellName)-1].'1');//合并单元格(如果要拆分单元格是需要先合并再拆分的,否则程序会报错) $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1',$top_title); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); }else{ $topNumber=1; } //处理表头 foreach ($cellName as $k=>$v) { $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellKey[$k].$topNumber, $v[1]);//设置表头数据 //$objPHPExcel->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//冻结窗口,作用不详,会出现一条竖线 $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//设置是否加粗 $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中 if($v[3] > 0)//大于0表示需要设置宽度 { $objPHPExcel->getActiveSheet()->getColumnDimension($cellKey[$k])->setWidth($v[3]);//设置列宽度 } } //处理数据 foreach ($data as $k=>$v) { foreach ($cellName as $k1=>$v1) { $objPHPExcel->getActiveSheet()->setCellValue($cellKey[$k1].($k+1+$topNumber), $v[$v1[0]]); if($v['end'] > 0) { if($v1[2] == 1)//这里表示合并单元格 { $objPHPExcel->getActiveSheet()->mergeCells($cellKey[$k1].$v['start'].':'.$cellKey[$k1].$v['end']); $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].$v['start'])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); } } if($v1[4] != "" && in_array($v1[4], array("LEFT","CENTER","RIGHT"))) { $v1[4] = eval('return \PHPExcel_Style_Alignment::HORIZONTAL_'.$v1[4].';'); //这里也可以直接传常量定义的值,即left,center,right;小写的strtolower $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].($k+1+$topNumber))->getAlignment()->setHorizontal($v1[4]); } } } //导出execl ob_end_clean();//清除缓冲区,避免乱码 header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"'); header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; }