<?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=mysql_query("SELECT * FROM tablas WHERE lower(tabla)='departamento' and id=".trim($_SESSION['id_validar']));
	while($row = mysql_fetch_array($sql)){
		$dpto[$row['codigo']]=$row['detalle'];
	}
	
	$sql=mysql_query("SELECT * FROM tablas WHERE lower(tabla)='cargo' and id=".trim($_SESSION['id_validar']));
	while($row = mysql_fetch_array($sql)){
		$cargo[$row['codigo']]=$row['detalle'];
	}

	$registros=mysql_query("select * from $sabana order by cedula");
	$cant=mysql_num_fields($registros) or die(mysql_error());
	$casig='';$cdedu='';
	for($f=0;$f<$cant;$f++)
	{
		$valor=mysql_field_name($registros,$f);
		if (strtoupper(substr($valor,0,1))=='A' || strtoupper(substr($valor,0,1))=='D') {
			$cod=trim(substr(mysql_field_name($registros,$f),1,50));
			$consulta=mysql_query("select detalle from concepto where id='".$_SESSION['id_validar']."' and codigo='".$cod."'");
			while($row=mysql_fetch_array($consulta)) $valor = $row['detalle'];
		}
		$header[]=strtoupper($valor);
	}
	
	
    $i=8;
    $cedula="";	
	 $j=0;
	$letra='A';
	while($fila = mysql_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 = mysql_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()->se‌​tRowHeight(-1);  
	//$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHe‌​ight(-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;
