码迷,mamicode.com
首页 > 其他好文 > 详细

导出报表excel

时间:2015-05-11 10:19:34      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

/* +----------------------------------------------------------------------
   | introduce:数据导出excel
   +----------------------------------------------------------------------
   | @param $config 表头,文件名等相关配置信息
   +----------------------------------------------------------------------
   | @param $data array 所要导出的结果集
   +----------------------------------------------------------------------
   | Author: liwuming
   +----------------------------------------------------------------------
*/
function exportExcel($config,$data){
	include ‘./PHPExcel/PHPExcel.PHPExcel‘;
	$objPHPExcel = new PHPExcel();
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);

	$column = 2;
	
	foreach($data as $key => $rows){ //行写入
		$span = ord("A");
		$i = 0;
		foreach($rows as $keyName=>$value){// 列写入
			$j = chr($span);
			$objPHPExcel->getActiveSheet()->setCellValue($j.$column, $value);
			if($config[‘center‘][$i]){
				$objPHPExcel->getActiveSheet()->getStyle($j.$column)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			}else{
				$objPHPExcel->getActiveSheet()->getStyle($j.$column)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			}
			
			$span++;
			$i++;
		}
		$column++;
	}
	
	//设置sheet名字
	if(!empty($config[‘title‘])){
		$objPHPExcel->getActiveSheet()->setTitle($config[‘title‘]);  
	}
	
	//设置默认行高
	$height = !empty($config[‘height‘])?$config[‘height‘]:30;
	$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight($height);
	
	//设置默认字体大小
	$objPHPExcel->getActiveSheet()->getDefaultStyle(‘A1:Z1‘)->getFont()->setSize(14); 
	$objPHPExcel->getActiveSheet()->getStyle(‘A1:Z1‘)->getFont()->setBold(true);  

	//设置表头
	$key = ord("A"); 		//返回字符的 ASCII 码值
	$i = 0;
	foreach($config[‘header‘] as $v){
		$colum = chr($key);
		$objPHPExcel->getActiveSheet()->setCellValue($colum.‘1‘, $v);
		$objPHPExcel->getActiveSheet()->getColumnDimension($colum)->setWidth($config[‘width‘][$i]); 
		$objPHPExcel->getActiveSheet()->getStyle($colum.‘1‘)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$key +=1;
		$i++;
	}

	//设置文件名
	if($config[‘name‘] == ‘time‘ || $config[‘name‘] == ‘‘){
		$date = date("Y_m_d",time());
		$fileName .= "{$date}.xls";
	}else{
		$fileName .= "{$config[‘name‘]}.xls";
	}
	
	
	$fileName = iconv("gb2312","utf-8", $fileName); //iconv 文件编码转换
	
	
	header ( "Pragma: public" );
	header ( "Expires: 0" );
	header ( "Cache-Control:must-revalidate, post-check=0, pre-check=0" );
	header ( "Content-Type:application/force-download" );
	header ( "Content-Type:application/vnd.ms-execl;charset=utf-8" );
	header ( "Content-Type:application/octet-stream" );
	header ( "Content-Type:application/download" );
	header ( ‘Content-Disposition:attachment;filename="‘.$fileName.‘"‘);
	$objWriter->save(‘php://output‘);  
	exit();
}

 

导出报表excel

标签:

原文地址:http://www.cnblogs.com/liwuming/p/4493719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!