日期:2014-05-17  浏览次数:20504 次

phpexcel 生成表格


	/*
 	 * 导出excel表格
 	 * 根据统计标题信息、日期信息和excel表格标示信息
 	 * 先循环统计标题信息在循环每条统计下面按日期的具体信息
 	 * 进行单元格设置 合并 添加样式
 	 */
 	function _export($tipMsg = null, $date = null, $excelTitle = null){
 		$this->autoRender = false;
		App::import('Vendor', 'phpexcel', array('file' => 'PHPExcel.php'));
		App::import('Vendor', 'phpexcelwriter', array('file' => 'PHPExcel'.DS.'Writer'.DS.'Excel2007.php')); // loads PHPExcel/Writer/Excel2007.php
		$objPHPExcel = new PHPExcel();
		$excelName  = 'speiyou_'.date('y-m-d H:i:s').'.xls';//文件名字
		// 设置属性
		$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
									 ->setLastModifiedBy("Maarten Balliauw")
									 ->setTitle("Office 2007 XLSX Test Document")
									 ->setSubject("Office 2007 XLSX Test Document")
									 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
									 ->setKeywords("office 2007 openxml php")
									 ->setCategory("Test result file");
		//样式声明
		$objActSheet = $objPHPExcel->getActiveSheet();
		//设置样式字体
		$sharedStyle1 = new PHPExcel_Style();
		$sharedStyle1->applyFromArray(
			array('fill' 	=> array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('argb' => 'FFCCFFCC')),
				  'borders' => array('bottom'=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
									 'right'=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
									 'top'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN),
									 'left'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN)),
				  'font' => array('bold' => true,'color'=>array('argb' => '00000000')),									
				 ));		
		// 添加头部数据
		$objPHPExcel->setActiveSheetIndex(0)
		            ->setCellValue('A1', '模块分类')
		            ->setCellValue('B1', '点击代码')
		            ->setCellValue('C1', '代码名称');
		 foreach ($date as $tdk => $tdv) {
		 	$objPHPExcel->setActiveSheetIndex(0)
		 				->setCellValue($excelTitle[$tdk].'1', $date[$tdk]);
		 }
 		/*添加主要内容
 		 * 分类处理 tou(头部导航) banji(班级分类) fonepage(首页一屏) ftwopage(首页二屏) fthreepage(首页三屏)
 		 * bottom(底部) teacher(名师) ad(广告)
 		 */
		 $num = 2;//循环开始标记位           
 		 $datenum = count($date) - 1;
		 foreach($tipMsg as $k => $v) {
		 	if ($k == 'tou') {
		 		$tounum = $num + 1;
				$newnum = $num-1;
		 		$objPHPExcel->setActiveSheetIndex(0)
		 			->setCellValue('A2','头部导航');
		 		//合并单元格
		 		$objActSheet->mergeCells('A'.$num.':C'.$num);	
		 		$objActSheet->mergeCells('A2'.':A'.$newnum);
		 		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "A2:A".$newnum);	
		 		$objPHPExcel->setActiveSheetIndex(0)
		 			->setCellValue('A'.$num, '本类汇总');
		 		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "A".$num.":".$excelTitle[count($date) - 1].$num);	
		 		$objActSheet->getStyle('A'.$num)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
 				//求和
				foreach($date as $datek => $datev) {
				$objPHPExcel->setActiveSheetIndex(0)
						->setCellValue($excelTitle[$datek].$num,'=SUM('.$excelTitle[$datek].'2:'.$excelTitle[$datek].$newnum.')');
			}		 		
		 	} elseif ($k == 'banji') {
		 		$banjinum = $num + 1;
				$newnum = $num-1;
		 		$objPHPExcel->setActiveSheetIndex(0)
		 			->setCellValue('A'.$tounum,'班级分类');
		 		//合并单元格
		 		$objActSheet->mergeCells('A'.$num.':C'.$num);
		 		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "A".$tounum.":A".$newnum);
		 		$objPHPExcel->setActiveSheetIndex(0)
		 			->setCellValue('A'.$num, '本类汇总');
		 		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "A".$num.":".$excelTitle[count($date) - 1].$num);
		 		$objActSheet->getStyle('A'.$num)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);		
	 			//求和
				foreach($date