<?php
include_once ("includes/setup.inc.php");

$kriteria = explode('|BRE|', fetch_postget_val("kriteria", $_POST, $_GET));
$allTable = explode(',', fetch_postget_val("allTable", $_POST, $_GET));
$allRow = array();
for($num = 0 ; $num < count($allTable) - 1 ; $num++)
{
	$allRow[] = explode('|BRE|', fetch_postget_val($allTable[$num], $_POST, $_GET));
}

$jenis_program = explode('|', $allRow[0][2]);
$columnSpan = count($jenis_program);
$temp = explode('|', $allRow[0][2]);
$kadun = $temp[0];


$AUTHOR = 'jj';
$TITLE = $kriteria[0];
$FILE_NAME = str_replace(' ', '_', $kriteria[0]);
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once 'classes/PHPExcel.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator($AUTHOR)
							 ->setLastModifiedBy($AUTHOR)
							 ->setTitle($TITLE)
							 ->setSubject($TITLE)
							 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
							 ->setKeywords("office 2007 openxml $TITLE")
							 ->setCategory("Test result file");

							 
//set default style
$objPHPExcel->getDefaultStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);

// Add data
//One can use the possibility to access cell values by column and row index like (0,1) instead of 'A1' for reading and writing cell values in loops.
//In PHPExcel column index is 0-based while row index is 1-based. That means 'A1' ~ (0,1)
$objPHPExcel->setActiveSheetIndex(0);
$ROW_INDEX = 1; //use this to control row indexing

//print table header
$START_COLUMN = 0; //normally start at 0
$END_COLUMN = count($jenis_program) + $START_COLUMN - 1; //based on number of header

$START_COLUMN_ALPHABET = PHPExcel_Cell::stringFromColumnIndex($START_COLUMN); //convert to alphabet
$END_COLUMN_ALPHABET = PHPExcel_Cell::stringFromColumnIndex($END_COLUMN); //convert to alphabet

//set anything before print table
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($START_COLUMN, $ROW_INDEX++, $kriteria[0]);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($START_COLUMN, $ROW_INDEX++, 'Kriteria Carian :');

$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($START_COLUMN, $ROW_INDEX, "Jenis : ");
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($START_COLUMN + 1, $ROW_INDEX++, $kriteria[1]);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($START_COLUMN, $ROW_INDEX, "Program : ");
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($START_COLUMN + 1, $ROW_INDEX++, $kriteria[2]);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($START_COLUMN, $ROW_INDEX, "Tahun : ");
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($START_COLUMN + 1, $ROW_INDEX++, $kriteria[3]);

$ROW_INDEX++; //move to next row
$ROW_INDEX++; //move to next row

for($num = 0 ; $num < count($allRow); $num++)
{
	$START_ROW = $ROW_INDEX; //keep a record of start row of table
	
	//other data
	for($num4 = 1 ; $num4 < count($allRow[$num]) ; $num4++)
	{
		$temp = explode('|', $allRow[$num][$num4]);
		for($num5 = 0 ; $num5 < count($temp); $num5++)
		{
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($num5, $ROW_INDEX, $temp[$num5]);
		}
		$ROW_INDEX++;
	}
	/*
	$ROW_INDEX++;
	//LAST ROW (JUMLAH BESAR)
	$temp = explode('|', $allRow[$num][count($allRow[$num]) - 1]);
	$objPHPExcel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($START_COLUMN).$ROW_INDEX)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
	$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($START_COLUMN + 1, $ROW_INDEX, $temp[1]);
	$objPHPExcel->getActiveSheet()->mergeCells(PHPExcel_Cell::stringFromColumnIndex($START_COLUMN + 1).$ROW_INDEX.":".PHPExcel_Cell::stringFromColumnIndex($END_COLUMN).($ROW_INDEX));
	*/
	$ROW_INDEX--;
	$END_ROW = $ROW_INDEX; //keep a record of end row of table

	//set border and alignment for table
	$objPHPExcel->getActiveSheet()->getStyle($START_COLUMN_ALPHABET.$START_ROW.':'.$END_COLUMN_ALPHABET.$END_ROW)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
	$objPHPExcel->getActiveSheet()->getStyle($START_COLUMN_ALPHABET.$START_ROW.':'.$END_COLUMN_ALPHABET.$END_ROW)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$ROW_INDEX++;
	$ROW_INDEX++;
	
	//set bold for first row
	$objPHPExcel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($START_COLUMN).$START_ROW.':'.PHPExcel_Cell::stringFromColumnIndex($END_COLUMN).($START_ROW))->getFont()->setBold(true);
	$objPHPExcel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($START_COLUMN).$START_ROW.':'.PHPExcel_Cell::stringFromColumnIndex($END_COLUMN).($START_ROW))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('E0E0E0');
	//set bold for all first column
	$objPHPExcel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($START_COLUMN).$START_ROW.':'.PHPExcel_Cell::stringFromColumnIndex($START_COLUMN).$END_ROW)->getFont()->setBold(true);
	//set bold for last row
	$objPHPExcel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($START_COLUMN).($END_ROW).':'.PHPExcel_Cell::stringFromColumnIndex($END_COLUMN).($END_ROW))->getFont()->setBold(true);
	$objPHPExcel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($START_COLUMN).($END_ROW).':'.PHPExcel_Cell::stringFromColumnIndex($END_COLUMN).($END_ROW))->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('E0E0E0');
	
}

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Table Data');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

//set auto width
/*
for( $colNum = $START_COLUMN ; $colNum <= $END_COLUMN ; $colNum++ )
{
	$objPHPExcel->getActiveSheet()->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($colNum))->setAutoSize(true);
	
}*/

//$objPHPExcel->getActiveSheet()->getColumnDimension($START_COLUMN_ALPHABET.':'.$END_COLUMN_ALPHABET)->setAutoSize(true);

// Redirect output to a client抯 web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$FILE_NAME.'.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;
?>