<?php
session_start();
include('clases/clase.conexion.php'); 
include('funciones/variables.php');
include_once "Spreadsheet/Excel/Writer.php";//JJ
//include_once "Spreadsheet/Writer.php";
$VarCon = VarServer("MYSQLI");
$objExp = new clsConexion($VarCon[0],$VarCon[1],$VarCon[2],$VarCon[3],$VarCon[4]);
$objExp-> AbrirConexion();

if(isset($_GET[sql1])) {
	$sql1 = str_replace("\\' - \\'"," + ' - ' + ",$_GET[sql1]);;
	$sql1 = str_replace("\\","",$sql1);
	}
else
	$sql1 = $_SESSION[expor_sql_1];

if(isset($_GET[sql2])) {
	$sql2 = str_replace("\\' - \\'"," + ' - ' + ",$_GET[sql2]);;
	$sql2 = str_replace("\\","",$sql2);
	}
else
	$sql2 = $_SESSION[expor_sql_2];


$rs  = $objExp->Query($sql1);
$col = $objExp->NumField($rs);

// Create an instance
$xls = new Spreadsheet_Excel_Writer();
 
// Send HTTP headers to tell the browser what's coming
$xls->send($_GET[page].".xls");
 
// Escribimos nombre de la hoja
$sheet =& $xls->addWorksheet('Hoja1');
$negrita =& $xls->addFormat();
$negrita->setBold();
$format_decimal =& $xls->addFormat();
$format_decimal->setNumFormat("#,##0.00;[Red](#,##0.00)");
$format_entero =& $xls->addFormat();
$format_entero->setNumFormat("#,##0;[Red]#,##0");
$format_porcen =& $xls->addFormat();
$format_porcen->setNumFormat("0.00%");

// Escribimos Nombres de Columnas
for ($j=0;$j<$col;$j++) {
	$sheet->write(0,$j, $objExp->FieldName($rs,$j),$negrita);
	}

$i=1;
// Escribimos la Data
while ($row=$objExp->FetchArray($rs)) {
	for ($j=0;$j<$col;$j++) {
		if (!is_null($row[$j]))	{
			$type = $objExp->FieldType($rs,$j);
			if ($type=="char" || $type=="varchar" || $type=="string")
				$sheet->writeString($i,$j,$row[$j]);		
			elseif ($type=="int")
				$sheet->write($i,$j,$row[$j],$format_entero);
			elseif ($type=="real" || $type=="numeric") {
				$cab = $objExp->FieldName($rs,$j);		
				if (strstr($cab,"Porc"))
					$sheet->write($i,$j,$row[$j]/100,$format_porcen);
				else
					$sheet->write($i,$j,$row[$j],$format_decimal);
				}
			else			
				$sheet->write($i,$j,$row[$j]);
			}
		}
 	$i++;
 	}

$objExp-> CerrarConexion();
$objExp-> AbrirConexion();

// Escribimos la data del Pie
if(isset($sql2) && $sql2<>"") {		
	$format_decimal_pie =& $xls->addFormat();
	$format_decimal_pie->setNumFormat("#,##0.00;[Red](#,##0.00)");
	$format_decimal_pie->setBold();
	$format_entero_pie =& $xls->addFormat();
	$format_entero_pie->setNumFormat("#,##0;[Red]#,##0");
	$format_entero_pie->setBold();
	$format_porcen_pie =& $xls->addFormat();
	$format_porcen_pie->setNumFormat("0.00%");
	$format_porcen_pie->setBold();
	$rs2 = $objExp->Query($sql2);
	$row = $objExp->FetchArray($rs2);
	$col2= $objExp->NumField($rs2);
	$coldif = $col-$col2;
	for ($j=0;$j<$col2;$j++) {
		$type = $objExp->FieldType($rs2,$j);
		if ($type=="char" || $type=="varchar" || $type=="string")
			$sheet->writeString($i,$j+$coldif,$row[$j],$negrita);		
		elseif ($type=="int")
			$sheet->write($i,$j+$coldif,$row[$j],$format_entero_pie);
		elseif ($type=="real" || $type=="numeric")
			{
			$cab = $objExp->FieldName($rs,$j);		
			if (strstr($cab,"Porc"))
				$sheet->write($i,$j+$coldif,$row[$j]/100,$format_porcen_pie);
			else
				$sheet->write($i,$j+$coldif,$row[$j],$format_decimal_pie);
			}
		else
			$sheet->write($i,$j+$coldif,$row[$j],$negrita);
		}
	}
$xls->close();
$_SESSION[expor_sql_1]="";
$_SESSION[expor_sql_2]="";

?>
