File "generar_excel.php"
Full Path: C:/wamp64/www/porteria/consulta/generar_excel.php
File size: 2.45 KB
MIME-type: text/x-php
Charset: utf-8
<?php
include("../connection.php");
$con = connection();
// Verificar sesión
session_start();
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
header('Location: login.html');
exit;
}
// Obtener parámetros
$tipo_reporte = $_GET['tipo_reporte'] ?? '';
$fecha_inicio = $_GET['fecha_inicio'] ?? '';
$fecha_fin = $_GET['fecha_fin'] ?? '';
if (!$tipo_reporte || !$fecha_inicio || !$fecha_fin) {
die('Parámetros incompletos');
}
// Consulta según el tipo de reporte
switch ($tipo_reporte) {
case 'visitantes':
$titulo_reporte = 'Reporte de Visitantes';
$sql = "SELECT * FROM colaboradores
WHERE fecha BETWEEN '$fecha_inicio' AND '$fecha_fin 23:59:59'
ORDER BY fecha DESC";
$nombre_archivo = 'reporte_visitantes';
break;
case 'permisos':
$titulo_reporte = 'Reporte de Permisos';
$sql = "SELECT * FROM permisos
WHERE fecha BETWEEN '$fecha_inicio' AND '$fecha_fin 23:59:59'
ORDER BY fecha DESC";
$nombre_archivo = 'reporte_permisos';
break;
case 'vehiculos':
$titulo_reporte = 'Reporte de Vehículos';
$sql = "SELECT * FROM vehiculos
WHERE FECHA BETWEEN '$fecha_inicio' AND '$fecha_fin 23:59:59'
ORDER BY FECHA DESC";
$nombre_archivo = 'reporte_vehiculos';
break;
default:
die('Tipo de reporte no válido');
}
$query = mysqli_query($con, $sql);
// Configurar headers para descarga Excel
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="' . $nombre_archivo . '_' . date('Y-m-d') . '.xls"');
header('Pragma: no-cache');
header('Expires: 0');
// Generar contenido Excel
echo "<table border='1'>";
echo "<tr><th colspan='" . (mysqli_num_fields($query) + 2) . "' style='background-color: #ff7b00; color: white; font-size: 16px;'>$titulo_reporte</th></tr>";
echo "<tr><th colspan='" . (mysqli_num_fields($query) + 2) . "'>Periodo: $fecha_inicio al $fecha_fin</th></tr>";
// Encabezados de columnas
echo "<tr>";
while ($field = mysqli_fetch_field($query)) {
echo "<th style='background-color: #f2f2f2; font-weight: bold;'>" . ucfirst($field->name) . "</th>";
}
echo "</tr>";
// Datos
while ($row = mysqli_fetch_assoc($query)) {
echo "<tr>";
foreach ($row as $valor) {
echo "<td>" . ($valor ?? '') . "</td>";
}
echo "</tr>";
}
echo "</table>";
exit;
?>