<?php
session_start();

//設定語言
if (isset($_SESSION['lang'])){
	if ($_SESSION['lang'] == "chi"){
		require "language/chi.php";
	} else {
		require "language/eng.php";
	}
	$lang = $_SESSION['lang'];
} else {
	$lang="chi";
	require "language/chi.php";
}
//完成設定語言
require "common.php";
require_once "PHPExcel.php";

//獲取所有POST
foreach ($_POST as $key => $value) 	{ 
	$$key = $value;    //2個$$代表轉為VARIABLE
}
foreach ($_GET as $key => $value) 	{ 
	$$key = $value;    //2個$$代表轉為VARIABLE
}
//完成獲取所有POST
if(isset($_SESSION['STAFFCODE'])){
	$staffcode = $_SESSION['STAFFCODE'];
}

//設定學年
if (!isset($targetschoolyear)){
	$targetschoolyear = $todayschyr;
}
//完成設定學年
//設定計分方式

/*swimming gala
	$absscore = 1;  // 個人項目缺席扣分
	$attscore = 1;	// 個人項目出席並達標加分
	$singlerank = array("9","7","6","5","4","3","2","1"); //25米個人項目排名得分
	$singlerank = array("18","14","12","10","8","6","4","2");//50米或以上個人項目排名得分
	$houserank = array("36","28","30","20","16","12","8","2"); //社制項目排名得分
	$newrecscore = 2; //個人項目新記錄加分
*/

	$absscore = 1;  // 個人項目缺席扣分
	$attscore = 1;	// 個人項目出席並達標加分
	$singlerank = array("9","7","6","5","4","3","2","1"); //個人項目排名得分
	$houserank = array("18","14","12","10","8","6","4","2");//社制項目排名得分
	$newrecscore = 2; //個人項目新記錄加分
	$gradearray = array("A","B","C");
	$sexarray = array("M","F");
	$housearray = array("R","Y","B","G");
//完成設定計分方式

//get title
$sql = mysqli_query($splink,"Select EVENT_CHNAME, EVENT_ENNAME from SCHYEAR where activeyear = \"1\"");
while ($row = mysqli_fetch_row($sql)){
	if ($lang == "chi") {
		$titlename = mb_substr($row[0],0,11)."_x000A_".mb_substr($row[0],11);
	} else {
		$titlename = mb_substr($row[1],0,34)."_x000A_".mb_substr($row[1],35);
	}
}

//default style
//set text alignment for all cell
$centertext = array(
	'alignment' => array(
		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
	)
);

$allborder=array(	
	'borders' => array(
		'allborders'	=> array('style' => PHPExcel_Style_Border::BORDER_THIN)
	)
);

$leftrightborder = array(	
	'borders' => array(
		'left'	=> array('style' => PHPExcel_Style_Border::BORDER_THICK),
		'right'	=> array('style' => PHPExcel_Style_Border::BORDER_THICK)
	)
);


//COMPETITION_ID: 45-50 high jump....use special list

//all field list
if (isset($downloadallfieldlist) ){
	// Create new PHPExcel object
	$objPHPExcel = new PHPExcel();

	// Set document properties
	$objPHPExcel->getProperties()->setCreator("Li Ka lung")
							 ->setLastModifiedBy("Li Ka Lung");

	//set default font size
	$objPHPExcel->getDefaultStyle()->getFont()->setSize(12); 


	$sql = mysqli_query($splink,"Select keyid,CH_DES,STANDARD,MEASURE from COMPETITIONITEM where TRACKFIELD=\"2\"");
	$index = 0;	
	while ($row = mysqli_fetch_row($sql)){
		$asql = mysqli_query($splink,"Select A.STUID, B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE from APPLICATION_COMPETITION AS A INNER JOIN studentinfo AS B on A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\" ORDER BY B.CLASSCODE ASC, B.CLASSNO ASC");
		$noofstud = mysqli_num_rows($asql);
		if ($noofstud > 0 ){
			// Add new sheet
		//	$objWorkSheet = $objPHPExcel->createSheet($index); //Setting index when creating
			$PHPwork=$objPHPExcel->createSheet($index);

			if ($row[0] >= 45 && $row[0] <= 50 ) {
				// Add some data
				$objPHPExcel->setActiveSheetIndex($index)
						->setCellValue('A1', $txtclass)
						->setCellValue('B1', $txtclassno)
						->setCellValue('C1', $txthouse)
						->setCellValue('D1', $txtname)
						->setCellValue('E1', $txtclothcode)
						->setCellValue('F1', $txtattendance)
						->setCellValue('AN1', $txtfailtime)
						->setCellValue('AO1', $txtresult)
						->setCellValue('AP1', $txtrank);
						
				$newrow = 2;
				$line  = 0;
				while ($arow = mysqli_fetch_row($asql)){
					if (strlen($arow[4]) == 1) {
						$showno = "0".$arow[4];
					} else {
						$showno = $arow[4];
					}
					$objPHPExcel->setActiveSheetIndex($index)
							->setCellValue('A'.$newrow, $arow[3])
							->setCellValue('B'.$newrow, $arow[4])
							->setCellValue('C'.$newrow, $arow[5])
							->setCellValue('D'.$newrow, $arow[1])
							->setCellValue('E'.$newrow, $arow[5].$arow[3].$showno)
							->setCellValue('F'.$newrow, " ".$txtattended." / ".$txtapplyleave." / ".$txtabsent);
					
					$line++;
	
	/*
					if ($line == 17 && !is_int($noofstud/17) ) {
						$objPHPExcel->getActiveSheet()->setBreak( 'A' . $newrow, PHPExcel_Worksheet::BREAK_ROW );
						$line = 0;
					}
		*/			
						
					$newrow++;
				}
				$endline = $newrow -1;
					
				//set column width
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setWidth(5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('B')->setWidth(5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('C')->setWidth(3);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('D')->setWidth(10);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('E')->setWidth(8);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('F')->setWidth(16.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('G')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('H')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('I')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('J')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('K')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('L')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('M')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('N')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('O')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('P')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('Q')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('R')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('S')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('T')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('U')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('V')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('W')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('X')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('Y')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('Z')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AA')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AB')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AC')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AD')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AE')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AF')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AG')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AH')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AI')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AJ')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AK')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AL')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AM')->setWidth(1.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AN')->setWidth(10);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AO')->setWidth(10);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('AP')->setWidth(10);
				
				
				//merge cell
				$objPHPExcel->setActiveSheetIndex($index)->mergeCells('G1:I1');
				$objPHPExcel->setActiveSheetIndex($index)->mergeCells('J1:L1');
				$objPHPExcel->setActiveSheetIndex($index)->mergeCells('M1:O1');
				$objPHPExcel->setActiveSheetIndex($index)->mergeCells('P1:R1');
				$objPHPExcel->setActiveSheetIndex($index)->mergeCells('S1:U1');
				$objPHPExcel->setActiveSheetIndex($index)->mergeCells('V1:X1');
				$objPHPExcel->setActiveSheetIndex($index)->mergeCells('Y1:AA1');
				$objPHPExcel->setActiveSheetIndex($index)->mergeCells('AB1:AD1');
				$objPHPExcel->setActiveSheetIndex($index)->mergeCells('AE1:AG1');
				$objPHPExcel->setActiveSheetIndex($index)->mergeCells('AH1:AJ1');
				$objPHPExcel->setActiveSheetIndex($index)->mergeCells('AK1:AM1');
	
				$objPHPExcel->getActiveSheet()->getStyle('A1:AP'.$endline)->applyFromArray($allborder);
				$objPHPExcel->getActiveSheet()->getStyle('G1:I'.$endline)->applyFromArray($leftrightborder);
				$objPHPExcel->getActiveSheet()->getStyle('J1:L'.$endline)->applyFromArray($leftrightborder);
				$objPHPExcel->getActiveSheet()->getStyle('M1:O'.$endline)->applyFromArray($leftrightborder);
				$objPHPExcel->getActiveSheet()->getStyle('P1:R'.$endline)->applyFromArray($leftrightborder);
				$objPHPExcel->getActiveSheet()->getStyle('S1:U'.$endline)->applyFromArray($leftrightborder);
				$objPHPExcel->getActiveSheet()->getStyle('V1:X'.$endline)->applyFromArray($leftrightborder);
				$objPHPExcel->getActiveSheet()->getStyle('Y1:AA'.$endline)->applyFromArray($leftrightborder);
				$objPHPExcel->getActiveSheet()->getStyle('AB1:AD'.$endline)->applyFromArray($leftrightborder);
				$objPHPExcel->getActiveSheet()->getStyle('AE1:AG'.$endline)->applyFromArray($leftrightborder);
				$objPHPExcel->getActiveSheet()->getStyle('AH1:AJ'.$endline)->applyFromArray($leftrightborder);
				$objPHPExcel->getActiveSheet()->getStyle('AK1:AM'.$endline)->applyFromArray($leftrightborder);

			} else {
				// Add some data
				$objPHPExcel->setActiveSheetIndex($index)
						->setCellValue('A1', $txtclass)
						->setCellValue('B1', $txtclassno)
						->setCellValue('C1', $txthouse)
						->setCellValue('D1', $txtname)
						->setCellValue('E1', $txtclothcode)
						->setCellValue('F1', $txtattendance)
						->setCellValue('G1', $txt1stheat."1")
						->setCellValue('H1', $txt1stheat."2")
						->setCellValue('I1', $txt1stheat."3")
						->setCellValue('J1', "")
						->setCellValue('K1', $txtfinal."1")
						->setCellValue('L1', $txtfinal."2")
						->setCellValue('M1', $txtfinal."3")
						->setCellValue('N1', "")
						->setCellValue('O1', $txtresult)
						->setCellValue('P1', $txtrank);
				$newrow = 2;
				$line  = 0;
				while ($arow = mysqli_fetch_row($asql)){
					if (strlen($arow[5]) == 1) {
						$showno = "0".$arow[4];
					} else {
						$showno = $arow[4];
					}
					$objPHPExcel->setActiveSheetIndex($index)
							->setCellValue('A'.$newrow, $arow[3])
							->setCellValue('B'.$newrow, $arow[4])
							->setCellValue('C'.$newrow, $arow[5])
							->setCellValue('D'.$newrow, $arow[1])
							->setCellValue('E'.$newrow, $arow[5].$arow[3].$showno)
							->setCellValue('F'.$newrow, " ".$txtattended." / ".$txtapplyleave." / ".$txtabsent);
					
					$line++;
	
	
				/*	if ($line == 17 && !is_int($noofstud/17) ) {
						$objPHPExcel->getActiveSheet()->setBreak( 'A' . $newrow, PHPExcel_Worksheet::BREAK_ROW );
						$line = 0;
					}
				*/	
						
					$newrow++;
				}
				$endline = $newrow -1;
				

	
				//set column width
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setWidth(5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('B')->setWidth(5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('C')->setWidth(3);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('D')->setWidth(10);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('E')->setWidth(8);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('F')->setWidth(16.5);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('G')->setWidth(10);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('H')->setWidth(10);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('I')->setWidth(10);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('J')->setWidth(1);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('K')->setWidth(10);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('L')->setWidth(10);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('M')->setWidth(10);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('N')->setWidth(1);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('O')->setWidth(10);
				$objPHPExcel->getActiveSheet($index)->getColumnDimension('P')->setWidth(10);
	
				//set border
				$objPHPExcel->getActiveSheet()->getStyle('A1:P'.$endline)->applyFromArray($allborder);
				
			}
			// set 1st row repeat every 
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1,1);

			
			// set paper size and direction
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setHorizontalCentered(true);

		
			$objPHPExcel->getDefaultStyle()->applyFromArray($centertext);
			
			//set column height
			$objPHPExcel->getActiveSheet($index)->getDefaultRowDimension()->setRowHeight(20);			//get standard n record

			//get best record
			$bestrecord= "";
			$srsql = mysqli_query($splink,"Select RECORD from BEST_RECORD where COMPETITION_ID=\"".$row[0]."\"");
			while ($srrow = mysqli_fetch_row($srsql)){
				$bestrecord = $srrow[0];
			}
			//get eventdate
			$eventdate="";
			$eventid="";
			$edsql = mysqli_query($splink,"Select DATE, ORDER_ID from COMPETITION_DATEID where COMPETITION_ID=\"".$row[0]."\" AND HeatorFinal=\"1\"");
			while ($edrow = mysqli_fetch_row($edsql)){
				$eventdate = $edrow[0];
				$eventid = $edrow[1];
			}
			
			// set header and footer
			$objPHPExcel->getActiveSheet($index)
//				->getHeaderFooter()->setOddHeader('&L'.$txteventid.':'.$eventid.'&C&26'.$company.'_x000A_&20'.$row[1].'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':'.$eventdate);
				->getHeaderFooter()->setOddHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'      '.$txtmeasurestandard.':'.$row[3].'&R'.$txteventday.':&26 '.$eventdate);

			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setEvenHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'      '.$txtmeasurestandard.':'.$row[3].'&R'.$txteventday.':&26 '.$eventdate);

			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setOddFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setEvenFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');

			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setRight(.2);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setLeft(.2);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setTop(2.4);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setBottom(1);
			
			// set title of the sheet
			$objPHPExcel->getActiveSheet($index)->setTitle($row[1]);
			
			
			$index++;
			

		}
	}
	$objPHPExcel->setActiveSheetIndexByName('Worksheet');
	$sheetIndex = $objPHPExcel->getActiveSheetIndex();
	$objPHPExcel->removeSheetByIndex($sheetIndex);

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$txtdownloadallfieldlist.'.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

}
//finish all field list

//all track with no set lane list
if (isset($donwloadalltracknolanelist) ){
	// Create new PHPExcel object
	$objPHPExcel = new PHPExcel();

	// Set document properties
	$objPHPExcel->getProperties()->setCreator("Li Ka Lung")
							 ->setLastModifiedBy("Li Ka Lung");

	//set default font size
	$objPHPExcel->getDefaultStyle()->getFont()->setSize(18); 


	$sql = mysqli_query($splink,"Select keyid,CH_DES, STANDARD from COMPETITIONITEM where TRACKFIELD=\"1\" AND SETLANE=\"0\" AND INTERHOUSE=\"0\"");
	$index = 0;	
	while ($row = mysqli_fetch_row($sql)){
		$asql = mysqli_query($splink,"Select A.STUID, B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE from APPLICATION_COMPETITION AS A INNER JOIN studentinfo AS B on A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\" ORDER BY B.CLASSCODE ASC, B.CLASSNO ASC");
		$noofstud = mysqli_num_rows($asql);
		if ($noofstud > 0 ){
			// Add new sheet
		//	$objWorkSheet = $objPHPExcel->createSheet($index); //Setting index when creating
			$PHPwork=$objPHPExcel->createSheet($index);



			// Add some data
			$objPHPExcel->setActiveSheetIndex($index)
					->setCellValue('A1', $txtclass)
					->setCellValue('B1', $txtclassno)
					->setCellValue('C1', $txthouse)
					->setCellValue('D1', $txtname)
					->setCellValue('E1', $txtclothcode)
					->setCellValue('F1', $txtattendance)
					->setCellValue('G1', $txtrank)
					->setCellValue('H1', $txtresult);
			$newrow = 2;
			while ($arow = mysqli_fetch_row($asql)){
					if (strlen($arow[4]) == 1) {
						$showno = "0".$arow[4];
					} else {
						$showno = $arow[4];
					}

				$objPHPExcel->setActiveSheetIndex($index)
						->setCellValue('A'.$newrow, $arow[3])
						->setCellValue('B'.$newrow, $arow[4])
						->setCellValue('C'.$newrow, $arow[5])
						->setCellValue('D'.$newrow, $arow[1])
						->setCellValue('E'.$newrow, $arow[5].$arow[3].$showno)
						->setCellValue('F'.$newrow, " ".$txtattended." / ".$txtapplyleave." / ".$txtabsent);
				$newrow++;
			}
			$endline = $newrow -1;

			

			//set column width
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('B')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('C')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('D')->setWidth(10);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('E')->setWidth(8);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('F')->setWidth(17);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('G')->setWidth(6);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('H')->setWidth(16);

			//set border
			$objPHPExcel->getActiveSheet()->getStyle('A1:H'.$endline)->applyFromArray($allborder);


			// set 1st row repeat every 
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1,1);

			
			// set paper size and direction
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setHorizontalCentered(true);
			
			//set default cell text style
			$objPHPExcel->getDefaultStyle()->applyFromArray($centertext);
			
			//set column height
			$objPHPExcel->getActiveSheet($index)->getDefaultRowDimension()->setRowHeight(30);			//get standard n record

			//get best record
			$bestrecord= "";
			$srsql = mysqli_query($splink,"Select RECORD from BEST_RECORD where COMPETITION_ID=\"".$row[0]."\"");
			while ($srrow = mysqli_fetch_row($srsql)){
				$bestrecord = $srrow[0];
			}
			//get eventdate
			$eventdate="";
			$eventid="";
			$edsql = mysqli_query($splink,"Select DATE, ORDER_ID from COMPETITION_DATEID where COMPETITION_ID=\"".$row[0]."\" AND HeatorFinal =\"1\"");
			while ($edrow = mysqli_fetch_row($edsql)){
				$eventdate = $edrow[0];
				$eventid = $edrow[1];
			}
			
			// set header and footer
			$objPHPExcel->getActiveSheet($index)
//				->getHeaderFooter()->setOddHeader('&L'.$txteventid.':'.$eventid.'&C&26'.$company.'_x000A_&20'.$row[1].'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':'.$eventdate);
				->getHeaderFooter()->setOddHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);

			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setEvenHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);

			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setOddFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setEvenFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');

			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setRight(.2);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setLeft(.2);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setTop(2.4);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setBottom(1);
			
			//center table when print
			$objPHPExcel->setActiveSheetIndex($index)->getPageSetup()->setHorizontalCentered(true);
			
			
			// set title of the sheet
			$objPHPExcel->getActiveSheet($index)->setTitle($row[1]);
			$index++;
		}
	}
	$objPHPExcel->setActiveSheetIndexByName('Worksheet');
	$sheetIndex = $objPHPExcel->getActiveSheetIndex();
	$objPHPExcel->removeSheetByIndex($sheetIndex);

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$txtdownloadalltracknolanelist.'.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

}
//finish all track with no set lane list

//all track with lane (include 1stheat and those without final ) list
if (isset($donwloadalltrackneedlaneheatlist)) {
	// Create new PHPExcel object
	$objPHPExcel = new PHPExcel();

	// Set document properties
	$objPHPExcel->getProperties()->setCreator("Li Ka Lung")
							 ->setLastModifiedBy("Li Ka Lung");

	//set default font size
	$objPHPExcel->getDefaultStyle()->getFont()->setSize(14); 


	$sql = mysqli_query($splink,"Select B.keyid,B.CH_DES,B.STANDARD,A.EVENT_ID from TRACKEVENT_HEAT_LANE AS A inner join COMPETITIONITEM AS B ON A.COMPETITION_ID=B.keyid GROUP BY A.COMPETITION_ID, A.EVENT_ID");
	$index = 0;	
	while ($row = mysqli_fetch_row($sql)){
		$asql = mysqli_query($splink,"Select EVENT_ID from TRACKEVENT_HEAT_LANE where COMPETITION_ID=\"".$row[0]."\" GROUP BY EVENT_ID ORDER BY EVENT_ID DESC");
		$noofevent = mysqli_num_rows($asql);
		while ($arow = mysqli_fetch_row($asql)){
			$maxno = $arow[0];
			break;
		}
		$asql = mysqli_query($splink,"Select A.STUID, B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE,A.LANE_NO from TRACKEVENT_HEAT_LANE AS A INNER JOIN studentinfo AS B on A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\" ORDER BY LANE_NO");
		$noofstud = mysqli_num_rows($asql);
		$asql = mysqli_query($splink,"Select A.STUID, B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE,A.LANE_NO from TRACKEVENT_HEAT_LANE AS A INNER JOIN studentinfo AS B on A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\" AND A.EVENT_ID=\"".$row[3]."\" ORDER BY LANE_NO");
		if ($noofstud > 0 ){
			$showid = $noofevent - ( $maxno - $row[3] );
			if ($noofstud > 8 )  {
				$eventstatus = $txt1stheat."&12-".$txteventid.$showid.$txtpageof.$noofevent;
				$HeatorFinal=0;
			} else {
				$eventstatus = $txtfinal;
				$HeatorFinal=1;
			}
			// Add new sheet
		//	$objWorkSheet = $objPHPExcel->createSheet($index); //Setting index when creating
			$PHPwork=$objPHPExcel->createSheet($index);

			// Add some data
			$objPHPExcel->setActiveSheetIndex($index)
					->setCellValue('A1', $txttracknum)
					->setCellValue('B1', $txtclass)
					->setCellValue('C1', $txtclassno)
					->setCellValue('D1', $txtname)
					->setCellValue('E1', $txtclothcode)
					->setCellValue('F1', $txtattendance)
					->setCellValue('G1', $txtrank)
					->setCellValue('H1', $txtresult)
					->setCellValue('A2', '1')
					->setCellValue('A3', '2')
					->setCellValue('A4', '3')
					->setCellValue('A5', '4')
					->setCellValue('A6', '5')
					->setCellValue('A7', '6')
					->setCellValue('A8', '7')
					->setCellValue('A9', '8');

			while ($arow = mysqli_fetch_row($asql)){
				$rowno = $arow[6] +1;
				if (strlen($arow[4]) == 1) {
					$showno = "0".$arow[4];
				} else {
					$showno = $arow[4];
				}

				$objPHPExcel->setActiveSheetIndex($index)
						->setCellValue('B'.$rowno, $arow[3])
						->setCellValue('C'.$rowno, $arow[4])
						->setCellValue('D'.$rowno, $arow[1])
						->setCellValue('E'.$rowno, $arow[5].$arow[3].$showno)
						->setCellValue('F'.$rowno, " ".$txtattended." / ".$txtapplyleave." / ".$txtabsent." ");
			}
			

			//set column width
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('B')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('C')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('D')->setWidth(10);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('E')->setWidth(8);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('F')->setWidth(17);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('G')->setWidth(6);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('H')->setWidth(16);

			$objPHPExcel->getActiveSheet()->getStyle('A1:H9')->applyFromArray($allborder);
			
			// set paper size and direction
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setHorizontalCentered(true);
			
			//set default cell text style
			$objPHPExcel->getDefaultStyle()->applyFromArray($centertext);
			
			//set column height
			$objPHPExcel->getActiveSheet($index)->getDefaultRowDimension()->setRowHeight(30);			//get standard n record

			//get best record
			$bestrecord= "";
			$srsql = mysqli_query($splink,"Select RECORD from BEST_RECORD where COMPETITION_ID=\"".$row[0]."\"");
			while ($srrow = mysqli_fetch_row($srsql)){
				$bestrecord = $srrow[0];
			}
			//get eventdate
			$eventdate="";
			$eventid="";
			$edsql = mysqli_query($splink,"Select DATE, ORDER_ID from COMPETITION_DATEID where COMPETITION_ID=\"".$row[0]."\" AND HeatorFinal=\"".$HeatorFinal."\"");
			while ($edrow = mysqli_fetch_row($edsql)){
				$eventdate = $edrow[0];
				$eventid = $edrow[1];
			}
			
			
			
			// set header and footer
			$objPHPExcel->getActiveSheet($index)
//				->getHeaderFooter()->setOddHeader('&L'.$txteventid.':'.$eventid.'&C&26'.$company.'_x000A_&20'.$row[1].'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':'.$eventdate);
				->getHeaderFooter()->setOddHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'_x000A_'.$eventstatus.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);

			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setEvenHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'_x000A_'.$eventstatus.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);

			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setOddFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setEvenFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');

			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setRight(.2);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setLeft(.2);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setTop(2.4);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setBottom(1);
			
			//center table when print
			$objPHPExcel->setActiveSheetIndex($index)->getPageSetup()->setHorizontalCentered(true);
			
			
			// set title of the sheet
			$objPHPExcel->getActiveSheet($index)->setTitle($row[1]."-".$row[3]);
			
			$index++;
		}
	}
	$objPHPExcel->setActiveSheetIndexByName('Worksheet');
	$sheetIndex = $objPHPExcel->getActiveSheetIndex();
	$objPHPExcel->removeSheetByIndex($sheetIndex);

	// Redirect output to a client’s web browser (Excel2007)
	header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
	header('Content-Disposition: attachment;filename="'.$txtdonwloadalltrackneedlaneheatlist.'.xlsx"');
	header('Cache-Control: max-age=0');
	// If you're serving to IE 9, then the following may be needed
	header('Cache-Control: max-age=1');
	// If you're serving to IE over SSL, then the following may be needed
	header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
	header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
	header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
	header ('Pragma: public'); // HTTP/1.0
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
	$objWriter->save('php://output');
	exit;
}
//finish all track with lane (include 1stheat and those without final ) list


//download track final list ( have heats)
if (isset($donwloadtrackfinallist)){
	// Create new PHPExcel object
	$objPHPExcel = new PHPExcel();

	// Set document properties
	$objPHPExcel->getProperties()->setCreator("Li Ka Lung")
							 ->setLastModifiedBy("Li Ka Lung");

	//set default font size
	$objPHPExcel->getDefaultStyle()->getFont()->setSize(14); 


	$sql = mysqli_query($splink,"Select B.keyid,B.CH_DES,B.STANDARD from TRACKEVENT_FINAL_LANE AS A inner join COMPETITIONITEM AS B ON A.COMPETITION_ID=B.keyid GROUP BY A.COMPETITION_ID");
	$index = 0;	
	while ($row = mysqli_fetch_row($sql)){
		$asql = mysqli_query($splink,"Select A.STUID, B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE,A.LANE_NO from TRACKEVENT_FINAL_LANE AS A INNER JOIN studentinfo AS B on A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\"  ORDER BY LANE_NO");
		$noofstud=mysqli_num_rows($asql);
		if ($noofstud > 0 ){
			$eventstatus = $txtfinal;
			// Add new sheet
		//	$objWorkSheet = $objPHPExcel->createSheet($index); //Setting index when creating
			$PHPwork=$objPHPExcel->createSheet($index);

			// Add some data
			$objPHPExcel->setActiveSheetIndex($index)
					->setCellValue('A1', $txttracknum)
					->setCellValue('B1', $txtclass)
					->setCellValue('C1', $txtclassno)
					->setCellValue('D1', $txtname)
					->setCellValue('E1', $txtclothcode)
					->setCellValue('F1', $txtattendance)
					->setCellValue('G1', $txtrank)
					->setCellValue('H1', $txtresult)
					->setCellValue('A2', '1')
					->setCellValue('A3', '2')
					->setCellValue('A4', '3')
					->setCellValue('A5', '4')
					->setCellValue('A6', '5')
					->setCellValue('A7', '6')
					->setCellValue('A8', '7')
					->setCellValue('A9', '8');

			while ($arow = mysqli_fetch_row($asql)){
				$rowno = $arow[6] +1;
				if (strlen($arow[4]) == 1) {
					$showno = "0".$arow[4];
				} else {
					$showno = $arow[4];
				}

				$objPHPExcel->setActiveSheetIndex($index)
						->setCellValue('B'.$rowno, $arow[3])
						->setCellValue('C'.$rowno, $arow[4])
						->setCellValue('D'.$rowno, $arow[1])
						->setCellValue('E'.$rowno, $arow[5].$arow[3].$showno)
						->setCellValue('F'.$rowno, " ".$txtattended." / ".$txtapplyleave." / ".$txtabsent." ");
			}
			

			//set column width
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('B')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('C')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('D')->setWidth(10);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('E')->setWidth(8);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('F')->setWidth(17);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('G')->setWidth(6);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('H')->setWidth(16);

			$objPHPExcel->getActiveSheet()->getStyle('A1:H9')->applyFromArray($allborder);
			
			// set paper size and direction
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setHorizontalCentered(true);
			
			//set default cell text style
			$objPHPExcel->getDefaultStyle()->applyFromArray($centertext);
			
			//set column height
			$objPHPExcel->getActiveSheet($index)->getDefaultRowDimension()->setRowHeight(30);			//get standard n record

			//get best record
			$bestrecord= "";
			$srsql = mysqli_query($splink,"Select RECORD from BEST_RECORD where COMPETITION_ID=\"".$row[0]."\"");
			while ($srrow = mysqli_fetch_row($srsql)){
				$bestrecord = $srrow[0];
			}
			//get eventdate
			$eventdate="";
			$eventid="";
			$edsql = mysqli_query($splink,"Select DATE, ORDER_ID from COMPETITION_DATEID where COMPETITION_ID=\"".$row[0]."\" AND HeatorFinal=\"1\"");
			while ($edrow = mysqli_fetch_row($edsql)){
				$eventdate = $edrow[0];
				$eventid = $edrow[1];
			}
			
			
			
			// set header and footer
			$objPHPExcel->getActiveSheet($index)
//				->getHeaderFooter()->setOddHeader('&L'.$txteventid.':'.$eventid.'&C&26'.$company.'_x000A_&20'.$row[1].'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':'.$eventdate);
				->getHeaderFooter()->setOddHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'_x000A_'.$eventstatus.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);

			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setEvenHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'_x000A_'.$eventstatus.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);

			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setOddFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setEvenFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');

			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setRight(.2);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setLeft(.2);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setTop(2.4);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setBottom(1);
			
			//center table when print
			$objPHPExcel->setActiveSheetIndex($index)->getPageSetup()->setHorizontalCentered(true);
			
			
			// set title of the sheet
			$objPHPExcel->getActiveSheet($index)->setTitle($row[1]);
			
			$index++;
		}
	}
	$objPHPExcel->setActiveSheetIndexByName('Worksheet');
	$sheetIndex = $objPHPExcel->getActiveSheetIndex();
	$objPHPExcel->removeSheetByIndex($sheetIndex);

	// Redirect output to a client’s web browser (Excel2007)
	header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
	header('Content-Disposition: attachment;filename="'.$txtdonwloadtrackfinallist.'.xlsx"');
	header('Cache-Control: max-age=0');
	// If you're serving to IE 9, then the following may be needed
	header('Cache-Control: max-age=1');
	// If you're serving to IE over SSL, then the following may be needed
	header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
	header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
	header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
	header ('Pragma: public'); // HTTP/1.0
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
	$objWriter->save('php://output');
	exit;
}
//finish download track final list

//download all house competition list
if (isset($downloadinterhousetracklist)){
	// Create new PHPExcel object
	$objPHPExcel = new PHPExcel();

	// Set document properties
	$objPHPExcel->getProperties()->setCreator("Li Ka Lung")
							 ->setLastModifiedBy("Li Ka Lung");

	//set default font size
	$objPHPExcel->getDefaultStyle()->getFont()->setSize(14); 


	$sql = mysqli_query($splink,"Select B.keyid,B.CH_DES,B.STANDARD from INTERHOUSE_COMPETITION AS A inner join COMPETITIONITEM AS B ON A.COMPETITION_ID=B.keyid GROUP BY A.COMPETITION_ID");
	$index = 0;	
	while ($row = mysqli_fetch_row($sql)){
		$asql = mysqli_query($splink,"Select SCHHOUSE, TEAM, LANE_NO from INTERHOUSE_COMPETITION AS A INNER JOIN studentinfo AS B on A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\"  ORDER BY LANE_NO");
		$noofstud=mysqli_num_rows($asql);
		if ($noofstud > 0 ){
			$eventstatus = $txtfinal;
			// Add new sheet
		//	$objWorkSheet = $objPHPExcel->createSheet($index); //Setting index when creating
			$PHPwork=$objPHPExcel->createSheet($index);

			// Add some data
			$objPHPExcel->setActiveSheetIndex($index)
					->setCellValue('A1', $txttracknum)
					->setCellValue('B1', $txthouse)
					->setCellValue('C1', $txtclassno)
					->setCellValue('D1', $txtname)
					->setCellValue('E1', $txtclothcode)
					->setCellValue('F1', $txtattendance)
					->setCellValue('G1', $txtrank)
					->setCellValue('H1', $txtresult)
					->setCellValue('A2', '1')
					->setCellValue('A3', '2')
					->setCellValue('A4', '3')
					->setCellValue('A5', '4')
					->setCellValue('A6', '5')
					->setCellValue('A7', '6')
					->setCellValue('A8', '7')
					->setCellValue('A9', '8');

			while ($arow = mysqli_fetch_row($asql)){
				$rowno = $arow[6] +1;
				if (strlen($arow[4]) == 1) {
					$showno = "0".$arow[4];
				} else {
					$showno = $arow[4];
				}

				$objPHPExcel->setActiveSheetIndex($index)
						->setCellValue('B'.$rowno, $arow[3])
						->setCellValue('C'.$rowno, $arow[4])
						->setCellValue('D'.$rowno, $arow[1])
						->setCellValue('E'.$rowno, $arow[5].$arow[3].$showno)
						->setCellValue('F'.$rowno, " ".$txtattended." / ".$txtapplyleave." / ".$txtabsent." ");
			}
			

			//set column width
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('B')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('C')->setWidth(5);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('D')->setWidth(10);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('E')->setWidth(8);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('F')->setWidth(17);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('G')->setWidth(6);
			$objPHPExcel->getActiveSheet($index)->getColumnDimension('H')->setWidth(16);

			$objPHPExcel->getActiveSheet()->getStyle('A1:H9')->applyFromArray($allborder);
			
			// set paper size and direction
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
			$objPHPExcel->getActiveSheet($index)->getPageSetup()->setHorizontalCentered(true);
			
			//set default cell text style
			$objPHPExcel->getDefaultStyle()->applyFromArray($centertext);
			
			//set column height
			$objPHPExcel->getActiveSheet($index)->getDefaultRowDimension()->setRowHeight(30);			//get standard n record

			//get best record
			$bestrecord= "";
			$srsql = mysqli_query($splink,"Select RECORD from BEST_RECORD where COMPETITION_ID=\"".$row[0]."\"");
			while ($srrow = mysqli_fetch_row($srsql)){
				$bestrecord = $srrow[0];
			}
			//get eventdate
			$eventdate="";
			$eventid="";
			$edsql = mysqli_query($splink,"Select DATE, ORDER_ID from COMPETITION_DATEID where COMPETITION_ID=\"".$row[0]."\" AND HeatorFinal=\"1\"");
			while ($edrow = mysqli_fetch_row($edsql)){
				$eventdate = $edrow[0];
				$eventid = $edrow[1];
			}
			
			
			
			// set header and footer
			$objPHPExcel->getActiveSheet($index)
//				->getHeaderFooter()->setOddHeader('&L'.$txteventid.':'.$eventid.'&C&26'.$company.'_x000A_&20'.$row[1].'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':'.$eventdate);
				->getHeaderFooter()->setOddHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'_x000A_'.$eventstatus.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);

			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setEvenHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'_x000A_'.$eventstatus.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);

			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setOddFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
			$objPHPExcel->getActiveSheet($index)
				->getHeaderFooter()->setEvenFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');

			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setRight(.2);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setLeft(.2);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setTop(2.4);
			$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setBottom(1);
			
			//center table when print
			$objPHPExcel->setActiveSheetIndex($index)->getPageSetup()->setHorizontalCentered(true);
			
			
			// set title of the sheet
			$objPHPExcel->getActiveSheet($index)->setTitle($row[1]);
			
			$index++;
		}
	}
	$objPHPExcel->setActiveSheetIndexByName('Worksheet');
	$sheetIndex = $objPHPExcel->getActiveSheetIndex();
	$objPHPExcel->removeSheetByIndex($sheetIndex);

	// Redirect output to a client’s web browser (Excel2007)
	header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
	header('Content-Disposition: attachment;filename="'.$txtdownloadinterhousetracklist.'.xlsx"');
	header('Cache-Control: max-age=0');
	// If you're serving to IE 9, then the following may be needed
	header('Cache-Control: max-age=1');
	// If you're serving to IE over SSL, then the following may be needed
	header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
	header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
	header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
	header ('Pragma: public'); // HTTP/1.0
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
	$objWriter->save('php://output');
	exit;
}
//finish download all house competition list


//donwload single event list
if (isset($downloadsingleeventlist)){
	
	$temp = explode("-",$targetevent);
	$COMPETITION_ID=$temp[0];
	$HeatorFinal = $temp[1];
	$sql = mysqli_query($splink,"Select CH_DES, EN_DES, STANDARD, MEASURE, TRACKFIELD,SETLANE from COMPETITIONITEM where keyid=\"".$temp[0]."\"");
	$index=0;
	while ($row = mysqli_fetch_row($sql)){
		if ($lang == "chi" ) {
			$eventname = $row[0];
		} else {
			$eventname = $row[1];
		}
		if ($row[4] == 1) { 
			//track
			// Create new PHPExcel object
			$objPHPExcel = new PHPExcel();
		
			// Set document properties
			$objPHPExcel->getProperties()->setCreator("Li Ka Lung")
									 ->setLastModifiedBy("Li Ka Lung");
		
			//set default font size
			$objPHPExcel->getDefaultStyle()->getFont()->setSize(14); 
			if ($row[5] == 1 ) {
				//need set lane


				if ($HeatorFinal == 0 ) {
			
					$sql = mysqli_query($splink,"Select B.keyid,B.CH_DES,B.STANDARD,A.EVENT_ID from TRACKEVENT_HEAT_LANE AS A inner join COMPETITIONITEM AS B ON A.COMPETITION_ID=B.keyid where B.keyid=\"".$temp[0]."\" GROUP BY A.COMPETITION_ID, A.EVENT_ID");
					$index = 0;	
					while ($row = mysqli_fetch_row($sql)){
						$asql = mysqli_query($splink,"Select EVENT_ID from TRACKEVENT_HEAT_LANE where COMPETITION_ID=\"".$row[0]."\" GROUP BY EVENT_ID ORDER BY EVENT_ID DESC");
						$noofevent = mysqli_num_rows($asql);
						while ($arow = mysqli_fetch_row($asql)){
							$maxno = $arow[0];
							break;
						}
						$asql = mysqli_query($splink,"Select A.STUID, B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE,A.LANE_NO from TRACKEVENT_HEAT_LANE AS A INNER JOIN studentinfo AS B on A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\" ORDER BY LANE_NO");
						$noofstud = mysqli_num_rows($asql);
						$asql = mysqli_query($splink,"Select A.STUID, B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE,A.LANE_NO from TRACKEVENT_HEAT_LANE AS A INNER JOIN studentinfo AS B on A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\" AND A.EVENT_ID=\"".$row[3]."\" ORDER BY LANE_NO");
						if ($noofstud > 0 ){
							$showid = $noofevent - ( $maxno - $row[3] );
							if ($noofstud > 8 )  {
								$eventstatus = $txt1stheat."&12-".$txteventid.$showid.$txtpageof.$noofevent;
								$HeatorFinal=0;
							} else {
								$eventstatus = $txtfinal;
								$HeatorFinal=1;
							}
							// Add new sheet
						//	$objWorkSheet = $objPHPExcel->createSheet($index); //Setting index when creating
							$PHPwork=$objPHPExcel->createSheet($index);
				
							// Add some data
							$objPHPExcel->setActiveSheetIndex($index)
									->setCellValue('A1', $txttracknum)
									->setCellValue('B1', $txtclass)
									->setCellValue('C1', $txtclassno)
									->setCellValue('D1', $txtname)
									->setCellValue('E1', $txtclothcode)
									->setCellValue('F1', $txtattendance)
									->setCellValue('G1', $txtrank)
									->setCellValue('H1', $txtresult)
									->setCellValue('A2', '1')
									->setCellValue('A3', '2')
									->setCellValue('A4', '3')
									->setCellValue('A5', '4')
									->setCellValue('A6', '5')
									->setCellValue('A7', '6')
									->setCellValue('A8', '7')
									->setCellValue('A9', '8');
				
							while ($arow = mysqli_fetch_row($asql)){
								$rowno = $arow[6] +1;
								if (strlen($arow[4]) == 1) {
									$showno = "0".$arow[4];
								} else {
									$showno = $arow[4];
								}
				
								$objPHPExcel->setActiveSheetIndex($index)
										->setCellValue('B'.$rowno, $arow[3])
										->setCellValue('C'.$rowno, $arow[4])
										->setCellValue('D'.$rowno, $arow[1])
										->setCellValue('E'.$rowno, $arow[5].$arow[3].$showno)
										->setCellValue('F'.$rowno, " ".$txtattended." / ".$txtapplyleave." / ".$txtabsent." ");
							}
							
				
							//set column width
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setWidth(5);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('B')->setWidth(5);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('C')->setWidth(5);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('D')->setWidth(10);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('E')->setWidth(8);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('F')->setWidth(17);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('G')->setWidth(6);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('H')->setWidth(16);
				
							$objPHPExcel->getActiveSheet()->getStyle('A1:H9')->applyFromArray($allborder);
							
							// set paper size and direction
							$objPHPExcel->getActiveSheet($index)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
							$objPHPExcel->getActiveSheet($index)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
							$objPHPExcel->getActiveSheet($index)->getPageSetup()->setHorizontalCentered(true);
							
							//set default cell text style
							$objPHPExcel->getDefaultStyle()->applyFromArray($centertext);
							
							//set column height
							$objPHPExcel->getActiveSheet($index)->getDefaultRowDimension()->setRowHeight(30);			//get standard n record
				
							//get best record
							$bestrecord= "";
							$srsql = mysqli_query($splink,"Select RECORD from BEST_RECORD where COMPETITION_ID=\"".$row[0]."\"");
							while ($srrow = mysqli_fetch_row($srsql)){
								$bestrecord = $srrow[0];
							}
							//get eventdate
							$eventdate="";
							$eventid="";
							$edsql = mysqli_query($splink,"Select DATE, ORDER_ID from COMPETITION_DATEID where COMPETITION_ID=\"".$row[0]."\" AND HeatorFinal=\"".$HeatorFinal."\"");
							while ($edrow = mysqli_fetch_row($edsql)){
								$eventdate = $edrow[0];
								$eventid = $edrow[1];
							}
							
							
							
							// set header and footer
							$objPHPExcel->getActiveSheet($index)
				//				->getHeaderFooter()->setOddHeader('&L'.$txteventid.':'.$eventid.'&C&26'.$company.'_x000A_&20'.$row[1].'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':'.$eventdate);
								->getHeaderFooter()->setOddHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'_x000A_'.$eventstatus.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);
				
							$objPHPExcel->getActiveSheet($index)
								->getHeaderFooter()->setEvenHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'_x000A_'.$eventstatus.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);
				
							$objPHPExcel->getActiveSheet($index)
								->getHeaderFooter()->setOddFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
							$objPHPExcel->getActiveSheet($index)
								->getHeaderFooter()->setEvenFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
				
							$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setRight(.2);
							$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setLeft(.2);
							$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setTop(2.4);
							$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setBottom(1);
							
							//center table when print
							$objPHPExcel->setActiveSheetIndex($index)->getPageSetup()->setHorizontalCentered(true);
							
							
							// set title of the sheet
							$objPHPExcel->getActiveSheet($index)->setTitle($row[1]."-".$row[3]);
							
							$index++;
						}
					}

				} else {
					$sql = mysqli_query($splink,"Select B.keyid,B.CH_DES,B.STANDARD from TRACKEVENT_FINAL_LANE AS A inner join COMPETITIONITEM AS B ON A.COMPETITION_ID=B.keyid  WHERE B.keyid=\"".$temp[0]."\" GROUP BY A.COMPETITION_ID");
					$index = 0;	
					while ($row = mysqli_fetch_row($sql)){
						$asql = mysqli_query($splink,"Select A.STUID, B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE,A.LANE_NO from TRACKEVENT_FINAL_LANE AS A INNER JOIN studentinfo AS B on A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\"  ORDER BY LANE_NO");
						$noofstud=mysqli_num_rows($asql);
						if ($noofstud > 0 ){
							$eventstatus = $txtfinal;
							// Add new sheet
						//	$objWorkSheet = $objPHPExcel->createSheet($index); //Setting index when creating
							$PHPwork=$objPHPExcel->createSheet($index);
				
							// Add some data
							$objPHPExcel->setActiveSheetIndex($index)
									->setCellValue('A1', $txttracknum)
									->setCellValue('B1', $txtclass)
									->setCellValue('C1', $txtclassno)
									->setCellValue('D1', $txtname)
									->setCellValue('E1', $txtclothcode)
									->setCellValue('F1', $txtattendance)
									->setCellValue('G1', $txtrank)
									->setCellValue('H1', $txtresult)
									->setCellValue('A2', '1')
									->setCellValue('A3', '2')
									->setCellValue('A4', '3')
									->setCellValue('A5', '4')
									->setCellValue('A6', '5')
									->setCellValue('A7', '6')
									->setCellValue('A8', '7')
									->setCellValue('A9', '8');
				
							while ($arow = mysqli_fetch_row($asql)){
								$rowno = $arow[6] +1;
								if (strlen($arow[4]) == 1) {
									$showno = "0".$arow[4];
								} else {
									$showno = $arow[4];
								}
				
								$objPHPExcel->setActiveSheetIndex($index)
										->setCellValue('B'.$rowno, $arow[3])
										->setCellValue('C'.$rowno, $arow[4])
										->setCellValue('D'.$rowno, $arow[1])
										->setCellValue('E'.$rowno, $arow[5].$arow[3].$showno)
										->setCellValue('F'.$rowno, " ".$txtattended." / ".$txtapplyleave." / ".$txtabsent." ");
							}
							
				
							//set column width
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setWidth(5);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('B')->setWidth(5);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('C')->setWidth(5);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('D')->setWidth(10);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('E')->setWidth(8);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('F')->setWidth(17);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('G')->setWidth(6);
							$objPHPExcel->getActiveSheet($index)->getColumnDimension('H')->setWidth(16);
				
							$objPHPExcel->getActiveSheet()->getStyle('A1:H9')->applyFromArray($allborder);
							
							// set paper size and direction
							$objPHPExcel->getActiveSheet($index)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
							$objPHPExcel->getActiveSheet($index)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
							$objPHPExcel->getActiveSheet($index)->getPageSetup()->setHorizontalCentered(true);
							
							//set default cell text style
							$objPHPExcel->getDefaultStyle()->applyFromArray($centertext);
							
							//set column height
							$objPHPExcel->getActiveSheet($index)->getDefaultRowDimension()->setRowHeight(30);			//get standard n record
				
							//get best record
							$bestrecord= "";
							$srsql = mysqli_query($splink,"Select RECORD from BEST_RECORD where COMPETITION_ID=\"".$row[0]."\"");
							while ($srrow = mysqli_fetch_row($srsql)){
								$bestrecord = $srrow[0];
							}
							//get eventdate
							$eventdate="";
							$eventid="";
							$edsql = mysqli_query($splink,"Select DATE, ORDER_ID from COMPETITION_DATEID where COMPETITION_ID=\"".$row[0]."\" AND HeatorFinal=\"1\"");
							while ($edrow = mysqli_fetch_row($edsql)){
								$eventdate = $edrow[0];
								$eventid = $edrow[1];
							}
							
							
							
							// set header and footer
							$objPHPExcel->getActiveSheet($index)
				//				->getHeaderFooter()->setOddHeader('&L'.$txteventid.':'.$eventid.'&C&26'.$company.'_x000A_&20'.$row[1].'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':'.$eventdate);
								->getHeaderFooter()->setOddHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'_x000A_'.$eventstatus.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);
				
							$objPHPExcel->getActiveSheet($index)
								->getHeaderFooter()->setEvenHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'_x000A_'.$eventstatus.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);
				
							$objPHPExcel->getActiveSheet($index)
								->getHeaderFooter()->setOddFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
							$objPHPExcel->getActiveSheet($index)
								->getHeaderFooter()->setEvenFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
				
							$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setRight(.2);
							$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setLeft(.2);
							$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setTop(2.4);
							$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setBottom(1);
							
							//center table when print
							$objPHPExcel->setActiveSheetIndex($index)->getPageSetup()->setHorizontalCentered(true);
							
							
							// set title of the sheet
							$objPHPExcel->getActiveSheet($index)->setTitle($row[1]);
							
							$index++;
						}
					}
				}
				
			} else {
				// no need set lane



				$sql = mysqli_query($splink,"Select keyid,CH_DES, STANDARD from COMPETITIONITEM where TRACKFIELD=\"1\" AND SETLANE=\"0\" AND INTERHOUSE=\"0\" AND keyid=\"".$temp[0]."\"");
				$index = 0;	
				while ($row = mysqli_fetch_row($sql)){
					$asql = mysqli_query($splink,"Select A.STUID, B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE from APPLICATION_COMPETITION AS A INNER JOIN studentinfo AS B on A.STUID=B.STUID where A.COMPETITION_ID=\"".$row[0]."\" ORDER BY B.CLASSCODE ASC, B.CLASSNO ASC");
					$noofstud = mysqli_num_rows($asql);
					if ($noofstud > 0 ){
						// Add new sheet
					//	$objWorkSheet = $objPHPExcel->createSheet($index); //Setting index when creating
						$PHPwork=$objPHPExcel->createSheet($index);
			
			
			
						// Add some data
						$objPHPExcel->setActiveSheetIndex($index)
								->setCellValue('A1', $txtclass)
								->setCellValue('B1', $txtclassno)
								->setCellValue('C1', $txthouse)
								->setCellValue('D1', $txtname)
								->setCellValue('E1', $txtclothcode)
								->setCellValue('F1', $txtattendance)
								->setCellValue('G1', $txtrank)
								->setCellValue('H1', $txtresult);
						$newrow = 2;
						while ($arow = mysqli_fetch_row($asql)){
								if (strlen($arow[4]) == 1) {
									$showno = "0".$arow[4];
								} else {
									$showno = $arow[4];
								}
			
							$objPHPExcel->setActiveSheetIndex($index)
									->setCellValue('A'.$newrow, $arow[3])
									->setCellValue('B'.$newrow, $arow[4])
									->setCellValue('C'.$newrow, $arow[5])
									->setCellValue('D'.$newrow, $arow[1])
									->setCellValue('E'.$newrow, $arow[5].$arow[3].$showno)
									->setCellValue('F'.$newrow, " ".$txtattended." / ".$txtapplyleave." / ".$txtabsent);
							$newrow++;
						}
						$endline = $newrow -1;
			
						
			
						//set column width
						$objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setWidth(5);
						$objPHPExcel->getActiveSheet($index)->getColumnDimension('B')->setWidth(5);
						$objPHPExcel->getActiveSheet($index)->getColumnDimension('C')->setWidth(5);
						$objPHPExcel->getActiveSheet($index)->getColumnDimension('D')->setWidth(10);
						$objPHPExcel->getActiveSheet($index)->getColumnDimension('E')->setWidth(8);
						$objPHPExcel->getActiveSheet($index)->getColumnDimension('F')->setWidth(17);
						$objPHPExcel->getActiveSheet($index)->getColumnDimension('G')->setWidth(6);
						$objPHPExcel->getActiveSheet($index)->getColumnDimension('H')->setWidth(16);
			
						//set border
						$objPHPExcel->getActiveSheet()->getStyle('A1:H'.$endline)->applyFromArray($allborder);
			
			
						// set 1st row repeat every 
						$objPHPExcel->getActiveSheet($index)->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1,1);
			
						
						// set paper size and direction
						$objPHPExcel->getActiveSheet($index)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
						$objPHPExcel->getActiveSheet($index)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
						$objPHPExcel->getActiveSheet($index)->getPageSetup()->setHorizontalCentered(true);
						
						//set default cell text style
						$objPHPExcel->getDefaultStyle()->applyFromArray($centertext);
						
						//set column height
						$objPHPExcel->getActiveSheet($index)->getDefaultRowDimension()->setRowHeight(30);			//get standard n record
			
						//get best record
						$bestrecord= "";
						$srsql = mysqli_query($splink,"Select RECORD from BEST_RECORD where COMPETITION_ID=\"".$row[0]."\"");
						while ($srrow = mysqli_fetch_row($srsql)){
							$bestrecord = $srrow[0];
						}
						//get eventdate
						$eventdate="";
						$eventid="";
						$edsql = mysqli_query($splink,"Select DATE, ORDER_ID from COMPETITION_DATEID where COMPETITION_ID=\"".$row[0]."\" AND HeatorFinal =\"1\"");
						while ($edrow = mysqli_fetch_row($edsql)){
							$eventdate = $edrow[0];
							$eventid = $edrow[1];
						}
						
						// set header and footer
						$objPHPExcel->getActiveSheet($index)
			//				->getHeaderFooter()->setOddHeader('&L'.$txteventid.':'.$eventid.'&C&26'.$company.'_x000A_&20'.$row[1].'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':'.$eventdate);
							->getHeaderFooter()->setOddHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);
			
						$objPHPExcel->getActiveSheet($index)
							->getHeaderFooter()->setEvenHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'&C&26'.$titlename.'_x000A_&20'.$row[1].'_x000A_&16'.$txttrackrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':&26 '.$eventdate);
			
						$objPHPExcel->getActiveSheet($index)
							->getHeaderFooter()->setOddFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
						$objPHPExcel->getActiveSheet($index)
							->getHeaderFooter()->setEvenFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
			
						$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setRight(.2);
						$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setLeft(.2);
						$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setTop(2.4);
						$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setBottom(1);
						
						//center table when print
						$objPHPExcel->setActiveSheetIndex($index)->getPageSetup()->setHorizontalCentered(true);
						
						
						// set title of the sheet
						$objPHPExcel->getActiveSheet($index)->setTitle($row[1]);
						$index++;
					}
				}



			}
		} else {
			//field
			// Create new PHPExcel object
			$objPHPExcel = new PHPExcel();
		
			// Set document properties
			$objPHPExcel->getProperties()->setCreator("Li Ka Lung")
									 ->setLastModifiedBy("Li Ka Lung");
		
			//set default font size
			$objPHPExcel->getDefaultStyle()->getFont()->setSize(14); 


			$asql = mysqli_query($splink,"Select A.STUID, B.CHNAME, B.ENNAME, B.CLASSCODE, B.CLASSNO, B.SCHHOUSE from APPLICATION_COMPETITION AS A INNER JOIN studentinfo AS B on A.STUID=B.STUID where A.COMPETITION_ID=\"".$temp[0]."\" ORDER BY B.CLASSCODE ASC, B.CLASSNO ASC");
			$noofstud = mysqli_num_rows($asql);
			if ($noofstud > 0 ){
				// Add new sheet
			//	$objWorkSheet = $objPHPExcel->createSheet($index); //Setting index when creating
				$PHPwork=$objPHPExcel->createSheet($index);
	
				if ($temp[0] >= 45 && $temp[0] <= 50 ) {
					// Add some data
					$objPHPExcel->setActiveSheetIndex($index)
							->setCellValue('A1', $txtclass)
							->setCellValue('B1', $txtclassno)
							->setCellValue('C1', $txthouse)
							->setCellValue('D1', $txtname)
							->setCellValue('E1', $txtclothcode)
							->setCellValue('F1', $txtattendance)
							->setCellValue('AN1', $txtfailtime)
							->setCellValue('AO1', $txtresult)
							->setCellValue('AP1', $txtrank);
							
					$newrow = 2;
					$line  = 0;
					while ($arow = mysqli_fetch_row($asql)){
						if (strlen($arow[4]) == 1) {
							$showno = "0".$arow[4];
						} else {
							$showno = $arow[4];
						}
						$objPHPExcel->setActiveSheetIndex($index)
								->setCellValue('A'.$newrow, $arow[3])
								->setCellValue('B'.$newrow, $arow[4])
								->setCellValue('C'.$newrow, $arow[5])
								->setCellValue('D'.$newrow, $arow[1])
								->setCellValue('E'.$newrow, $arow[5].$arow[3].$showno)
								->setCellValue('F'.$newrow, " ".$txtattended." / ".$txtapplyleave." / ".$txtabsent);
						
						$line++;
		
		/*
						if ($line == 17 && !is_int($noofstud/17) ) {
							$objPHPExcel->getActiveSheet()->setBreak( 'A' . $newrow, PHPExcel_Worksheet::BREAK_ROW );
							$line = 0;
						}
			*/			
							
						$newrow++;
					}
					$endline = $newrow -1;
						
					//set column width
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setWidth(5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('B')->setWidth(5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('C')->setWidth(3);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('D')->setWidth(10);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('E')->setWidth(8);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('F')->setWidth(16.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('G')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('H')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('I')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('J')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('K')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('L')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('M')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('N')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('O')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('P')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('Q')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('R')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('S')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('T')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('U')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('V')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('W')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('X')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('Y')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('Z')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AA')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AB')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AC')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AD')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AE')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AF')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AG')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AH')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AI')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AJ')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AK')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AL')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AM')->setWidth(1.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AN')->setWidth(10);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AO')->setWidth(10);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('AP')->setWidth(10);
					
					
					//merge cell
					$objPHPExcel->setActiveSheetIndex($index)->mergeCells('G1:I1');
					$objPHPExcel->setActiveSheetIndex($index)->mergeCells('J1:L1');
					$objPHPExcel->setActiveSheetIndex($index)->mergeCells('M1:O1');
					$objPHPExcel->setActiveSheetIndex($index)->mergeCells('P1:R1');
					$objPHPExcel->setActiveSheetIndex($index)->mergeCells('S1:U1');
					$objPHPExcel->setActiveSheetIndex($index)->mergeCells('V1:X1');
					$objPHPExcel->setActiveSheetIndex($index)->mergeCells('Y1:AA1');
					$objPHPExcel->setActiveSheetIndex($index)->mergeCells('AB1:AD1');
					$objPHPExcel->setActiveSheetIndex($index)->mergeCells('AE1:AG1');
					$objPHPExcel->setActiveSheetIndex($index)->mergeCells('AH1:AJ1');
					$objPHPExcel->setActiveSheetIndex($index)->mergeCells('AK1:AM1');
		
					$objPHPExcel->getActiveSheet()->getStyle('A1:AP'.$endline)->applyFromArray($allborder);
					$objPHPExcel->getActiveSheet()->getStyle('G1:I'.$endline)->applyFromArray($leftrightborder);
					$objPHPExcel->getActiveSheet()->getStyle('J1:L'.$endline)->applyFromArray($leftrightborder);
					$objPHPExcel->getActiveSheet()->getStyle('M1:O'.$endline)->applyFromArray($leftrightborder);
					$objPHPExcel->getActiveSheet()->getStyle('P1:R'.$endline)->applyFromArray($leftrightborder);
					$objPHPExcel->getActiveSheet()->getStyle('S1:U'.$endline)->applyFromArray($leftrightborder);
					$objPHPExcel->getActiveSheet()->getStyle('V1:X'.$endline)->applyFromArray($leftrightborder);
					$objPHPExcel->getActiveSheet()->getStyle('Y1:AA'.$endline)->applyFromArray($leftrightborder);
					$objPHPExcel->getActiveSheet()->getStyle('AB1:AD'.$endline)->applyFromArray($leftrightborder);
					$objPHPExcel->getActiveSheet()->getStyle('AE1:AG'.$endline)->applyFromArray($leftrightborder);
					$objPHPExcel->getActiveSheet()->getStyle('AH1:AJ'.$endline)->applyFromArray($leftrightborder);
					$objPHPExcel->getActiveSheet()->getStyle('AK1:AM'.$endline)->applyFromArray($leftrightborder);
	
				} else {
					// Add some data
					$objPHPExcel->setActiveSheetIndex($index)
							->setCellValue('A1', $txtclass)
							->setCellValue('B1', $txtclassno)
							->setCellValue('C1', $txthouse)
							->setCellValue('D1', $txtname)
							->setCellValue('E1', $txtclothcode)
							->setCellValue('F1', $txtattendance)
							->setCellValue('G1', $txt1stheat."1")
							->setCellValue('H1', $txt1stheat."2")
							->setCellValue('I1', $txt1stheat."3")
							->setCellValue('J1', "")
							->setCellValue('K1', $txtfinal."1")
							->setCellValue('L1', $txtfinal."2")
							->setCellValue('M1', $txtfinal."3")
							->setCellValue('N1', "")
							->setCellValue('O1', $txtresult)
							->setCellValue('P1', $txtrank);
					$newrow = 2;
					$line  = 0;
					while ($arow = mysqli_fetch_row($asql)){
						if (strlen($arow[5]) == 1) {
							$showno = "0".$arow[4];
						} else {
							$showno = $arow[4];
						}
						$objPHPExcel->setActiveSheetIndex($index)
								->setCellValue('A'.$newrow, $arow[3])
								->setCellValue('B'.$newrow, $arow[4])
								->setCellValue('C'.$newrow, $arow[5])
								->setCellValue('D'.$newrow, $arow[1])
								->setCellValue('E'.$newrow, $arow[5].$arow[3].$showno)
								->setCellValue('F'.$newrow, " ".$txtattended." / ".$txtapplyleave." / ".$txtabsent);
						
						$line++;
		
		
					/*	if ($line == 17 && !is_int($noofstud/17) ) {
							$objPHPExcel->getActiveSheet()->setBreak( 'A' . $newrow, PHPExcel_Worksheet::BREAK_ROW );
							$line = 0;
						}
					*/	
							
						$newrow++;
					}
					$endline = $newrow -1;
					
	
		
					//set column width
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setWidth(5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('B')->setWidth(5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('C')->setWidth(3);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('D')->setWidth(10);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('E')->setWidth(8);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('F')->setWidth(16.5);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('G')->setWidth(10);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('H')->setWidth(10);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('I')->setWidth(10);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('J')->setWidth(1);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('K')->setWidth(10);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('L')->setWidth(10);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('M')->setWidth(10);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('N')->setWidth(1);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('O')->setWidth(10);
					$objPHPExcel->getActiveSheet($index)->getColumnDimension('P')->setWidth(10);
		
					//set border
					$objPHPExcel->getActiveSheet()->getStyle('A1:P'.$endline)->applyFromArray($allborder);
					
				}
				// set 1st row repeat every 
				$objPHPExcel->getActiveSheet($index)->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1,1);
	
				
				// set paper size and direction
				$objPHPExcel->getActiveSheet($index)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
				$objPHPExcel->getActiveSheet($index)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
				$objPHPExcel->getActiveSheet($index)->getPageSetup()->setHorizontalCentered(true);
	
			
				$objPHPExcel->getDefaultStyle()->applyFromArray($centertext);
				
				//set column height
				$objPHPExcel->getActiveSheet($index)->getDefaultRowDimension()->setRowHeight(20);			//get standard n record
	
				//get best record
				$bestrecord= "";
				$srsql = mysqli_query($splink,"Select RECORD from BEST_RECORD where COMPETITION_ID=\"".$temp[0]."\"");
				while ($srrow = mysqli_fetch_row($srsql)){
					$bestrecord = $srrow[0];
				}
				//get eventdate
				$eventdate="";
				$eventid="";
				$edsql = mysqli_query($splink,"Select DATE, ORDER_ID from COMPETITION_DATEID where COMPETITION_ID=\"".$temp[0]."\" AND HeatorFinal=\"1\"");
				while ($edrow = mysqli_fetch_row($edsql)){
					$eventdate = $edrow[0];
					$eventid = $edrow[1];
				}
				
				// set header and footer
				$objPHPExcel->getActiveSheet($index)
	//				->getHeaderFooter()->setOddHeader('&L'.$txteventid.':'.$eventid.'&C&26'.$company.'_x000A_&20'.$row[1].'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'&R'.$txteventday.':'.$eventdate);
					->getHeaderFooter()->setOddHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'&C&26'.$titlename.'_x000A_&20'.$eventname.'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'      '.$txtmeasurestandard.':'.$row[3].'&R'.$txteventday.':&26 '.$eventdate);
	
				$objPHPExcel->getActiveSheet($index)
					->getHeaderFooter()->setEvenHeader('&L'.$txttimetableeventno.':&26 '.$eventid.'&C&26'.$titlename.'_x000A_&20'.$eventname.'_x000A_&16'.$txtresultrecordlist.'_x000A_'.$txtstandard.':'.$row[2].'      '.$txtbestrecord.':'.$bestrecord.'      '.$txtmeasurestandard.':'.$row[3].'&R'.$txteventday.':&26 '.$eventdate);
	
				$objPHPExcel->getActiveSheet($index)
					->getHeaderFooter()->setOddFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
				$objPHPExcel->getActiveSheet($index)
					->getHeaderFooter()->setEvenFooter('&R'.$txtjudge.":________________x000A_".$txtpage.' &P '.$txtpageof.' &N ');
	
				$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setRight(.2);
				$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setLeft(.2);
				$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setTop(2.4);
				$objPHPExcel->setActiveSheetIndex($index)->getPageMargins()->setBottom(1);
				
				// set title of the sheet
				$objPHPExcel->getActiveSheet($index)->setTitle($eventname);
				
				
				$index++;
				
	
			}
		}
	}
	
	$objPHPExcel->setActiveSheetIndexByName('Worksheet');
	$sheetIndex = $objPHPExcel->getActiveSheetIndex();
	$objPHPExcel->removeSheetByIndex($sheetIndex);

	// Redirect output to a client’s web browser (Excel2007)
	header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
	header('Content-Disposition: attachment;filename="'.$eventname.'.xlsx"');
	header('Cache-Control: max-age=0');
	// If you're serving to IE 9, then the following may be needed
	header('Cache-Control: max-age=1');
	// If you're serving to IE over SSL, then the following may be needed
	header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
	header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
	header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
	header ('Pragma: public'); // HTTP/1.0
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
	$objWriter->save('php://output');
	exit;


}


//finish download single event list




