<?php
date_default_timezone_set('America/Caracas');
require_once 'PHPExcel/PHPExcel.php';

//PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
		// Se crea el objeto PHPExcel
		$objPHPExcel = new PHPExcel();
		

		// Se asignan las propiedades del libro
		$objPHPExcel->getProperties()->setCreator("Saireh") //Autor
							 ->setLastModifiedBy("Saireh") //Ultimo usuario que lo modificó
							 ->setTitle("Reporte Excel")
							 ->setSubject("Reporte Excel ")
							 ->setDescription("Reporte")
							 ->setKeywords("reporte")
							 ->setCategory("Reporte excel");
							 
	
    $head=array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => 'a5a5a5')),
		'font' => array('color'=> array('rgb' => '000000')));
	$header=array();
	$dpto=array();
	$cargo=array();
	
	$sql=mysqli_query($enlace,"SELECT * FROM tablas WHERE lower(tabla)='departamento' and id=".trim($_SESSION['id_validar']));
	while($row = mysqli_fetch_array($sql)){
		$dpto[$row['codigo']]=$row['detalle'];
	}
	
	$sql=mysqli_query($enlace,"SELECT * FROM tablas WHERE lower(tabla)='cargo' and id=".trim($_SESSION['id_validar']));
	while($row = mysqli_fetch_array($sql)){
		$cargo[$row['codigo']]=$row['detalle'];
	}

	$registros=mysqli_query($enlace,"select * from $sabana order by cedula");
	$cant=mysqli_num_fields($registros) or die(mysqli_error($enlace));
	$casig='';$cdedu='';
	for($f=0;$f<$cant;$f++)
	{
		$valor=mysqli_fetch_field_direct($registros,$f);
		if (strtoupper(substr($valor,0,1))=='A' || strtoupper(substr($valor,0,1))=='D') {
			$cod=trim(substr(mysqli_fetch_field_direct($registros,$f),1,50));
			$consulta=mysqli_query($enlace,"select detalle from concepto where id='".$_SESSION['id_validar']."' and codigo='".$cod."'");
			while($row=mysqli_fetch_array($consulta)) $valor = $row['detalle'];
		}
		$header[]=strtoupper($valor);
	}
	
	
    $i=8;
    $cedula="";	
	 $j=0;
	$letra='A';
	while($fila = mysqli_fetch_row($registros)) {	
         $j=0;	
		 foreach($fila as $valor) 
		  {  
		     switch($j){
				 case 2:$valor=$dpto[$valor];
				 break;
				 case 3:$valor=$cargo[$valor];
				 break;
				 default:break;
			 }
			 $objPHPExcel->getActiveSheet()->setCellValue(($letra++).$i,$valor);
			 $j++;
		  }
		  $letra='A';
		  $i++;
		  
	}
   /* while($row = mysqli_fetch_array($registros)){
		
			$objPHPExcel->getActiveSheet()
			->setCellValue("C".$i,$row['cedula'])
			->setCellValue("D".$i,$row['nombre']);
			$i++;
		
	}*/
    $letra='A';
	for($i=0;$i<count($header);$i++){
		$objPHPExcel->getActiveSheet()->setCellValue($letra."7",$header[$i]);
	    //$objPHPExcel->getActiveSheet()->getStyle($letra."1")->applyFromArray($estiloTituloReporte);
		$objPHPExcel->getActiveSheet()->getColumnDimension($letra)->setAutoSize(true);
		$letra++;
	}
	$letra--;
	$objPHPExcel->getActiveSheet()->getStyle('A7:'.$letra.'7')->applyFromArray($head);
	$objPHPExcel->getActiveSheet()->setCellValue('A1',' ');
	
	
	/*$objDrawing = new PHPExcel_Worksheet_Drawing();
	$objDrawing->setName('logo');
	$objDrawing->setDescription('logo');
	$objDrawing->setPath("images/logo_".trim($_SESSION['id_validar']).".jpg");
	$objDrawing->setResizeProportional(true);
	$objDrawing->setWidth(200);
	$objDrawing->setCoordinates('A1');
	$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());*/
    //$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(-1);  
	//$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
	
	$gdImage = imagecreatefromjpeg("images/logo_".trim($_SESSION['id_validar']).".jpg");
    $objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
    $objDrawing->setName('Logo');$objDrawing->setDescription('Logo');
    $objDrawing->setImageResource($gdImage);
    $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
    $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
	$objDrawing->setWidth(200);
	$objDrawing->setHeight(100);
	$objDrawing->setCoordinates('A1');
	$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
	$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(80);
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(320);

			 

	// Se asigna el nombre a la hoja
	$objPHPExcel->getActiveSheet()->setTitle('Nomina');
	// Se activa la hoja para que sea la que se muestre cuando el archivo se abre
	$objPHPExcel->setActiveSheetIndex(0);
	// Inmovilizar paneles 
	//$objPHPExcel->getActiveSheet(0)->freezePane('A4');
$nombrearch="Nomina";	
//header('Content-Type: application/vnd.ms-excel');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$nombrearch.'.xlsx"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
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, 'Excel5');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
