<?
if(!isset($_SESSION['usuario_p_prov_absa']) || trim($_SESSION['usuario_p_prov_absa']) == "" || trim(strtoupper($_SESSION["URL_PORTAL"]."admin.php")) != trim(strtoupper($_SERVER['HTTP_REFERER']))){
	?><script>window.location.href="<?=$_SESSION["URL_PORTAL"]."index.php"?>"</script><?
	exit;
}
$col_name[0]="A";
$col_name[1]="B";
$col_name[2]="C";
$col_name[3]="D";
$col_name[4]="E";
$col_name[5]="F";
$col_name[6]="G";
$col_name[7]="H";
$col_name[8]="I";
$col_name[9]="J";
$col_name[10]="K";
$col_name[11]="L";
$col_name[12]="M";
$col_name[13]="N";
$col_name[14]="O";
$col_name[15]="P";
$col_name[16]="Q";
$col_name[17]="R";
$col_name[18]="S";
$col_name[19]="T";
$col_name[20]="U";
$col_name[21]="V";
$col_name[22]="W";
$col_name[23]="X";
$col_name[24]="Y";
$col_name[25]="Z";
$col_name[26]="AA";
$col_name[27]="AB";
$col_name[28]="AC";
$col_name[29]="AD";
$col_name[30]="AE";
$col_name[31]="AF";
$col_name[32]="AG";
$col_name[33]="AH";
$col_name[34]="AI";
$col_name[35]="AJ";
$col_name[36]="AK";
$col_name[37]="AL";
$col_name[38]="AM";
$col_name[39]="AN";
$col_name[40]="AO";

//$detalle = explode("\n",$_POST['csvBuffer']);
$detalle = explode("\n",@$_SESSION['datosGrid'.@$_POST['noSesion']]);

$header_name = explode(",",$_POST['header']);
$ignorarCol = explode(",",$_POST['ignorarCol']);

$objPHPExcel->setActiveSheetIndex(0);
// Encabezado

$col=0;
$autofilter=0;
for($row=0; $row < count($header_name); $row++){
	if(!array_keys($ignorarCol,$row) || trim($_POST['ignorarCol'])==""){
		$objPHPExcel->getActiveSheet()->setCellValue($col_name[$col].'1', $header_name[$row]);
		$objPHPExcel->getActiveSheet()->getColumnDimension($col_name[$col])->setAutoSize(true);
		$autofilter++;
                $col++;
	}
}

$num_row=2;
for($d=0; $d < count($detalle); $d++){
		$detalle_2 = explode("\t",$detalle[$d]);
		$col=0;
	for($row=0; $row < count($detalle_2); $row++){
		if(!array_keys($ignorarCol,$row) || trim($_POST['ignorarCol'])==""){
			$objPHPExcel->getActiveSheet()->setCellValue($col_name[$col].$num_row, str_replace("*-*-*","\n",$detalle_2[$row]));
			$col++;
		}
	}
	$num_row++;
}
//header style
$objPHPExcel->getActiveSheet()->getStyle('A1:'.$col_name[$autofilter-1].'1')->applyFromArray(
		array(
			'font'    => array(
				'bold'      => true
			),
			'alignment' => array(
				'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			),
			'borders' => array(
				'top'     => array(
 					'style' => PHPExcel_Style_Border::BORDER_THIN
 				)
			),
			'fill' => array(
	 			'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
	  			'rotation'   => 90,
	 			'startcolor' => array(
	 				'argb' => 'FFA0A0A0'
	 			),
	 			'endcolor'   => array(
	 				'argb' => 'FFFFFFFF'
	 			)
	 		)
		)
);

//Autofilter
$objPHPExcel->getActiveSheet()->setAutoFilter('A1:'.$col_name[$autofilter-1].'1');

/*$objPHPExcel->getActiveSheet()->setCellValue('D1', PHPExcel_Shared_Date::PHPToExcel( gmmktime(0,0,0,date('m'),date('d'),date('Y')) ));
$objPHPExcel->getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15);
$objPHPExcel->getActiveSheet()->setCellValue('E1', '#12566');*/

$objPHPExcel->getActiveSheet()->setTitle($_POST['sheetName']);

function limpiar_caracteres_especiales($s) {
	$s = preg_replace("[áàâãª]","a",$s);
	$s = preg_replace("[ÁÀÂÃ]","A",$s);
	$s = preg_replace("[éèê]","e",$s);
	$s = preg_replace("[ÉÈÊ]","E",$s);
	$s = preg_replace("[íìî]","i",$s);
	$s = preg_replace("[ÍÌÎ]","I",$s);
	$s = preg_replace("[óòôõº]","o",$s);
	$s = preg_replace("[ÓÒÔÕ]","O",$s);
	$s = preg_replace("[úùû]","u",$s);
	$s = preg_replace("[ÚÙÛ]","U",$s);
	$s = str_replace(" ","-",$s);
	$s = str_replace("ñ","n",$s);
	$s = str_replace("Ñ","N",$s);
	//para ampliar los caracteres a reemplazar agregar lineas de este tipo:
	//$s = str_replace("caracter-que-queremos-cambiar","caracter-por-el-cual-lo-vamos-a-cambiar",$s);
	return $s;
}
?>