prepare($query); $stmt->execute(); $pedidos = $stmt->fetchAll(PDO::FETCH_ASSOC); header('Content-Type: application/vnd.ms-excel; charset=utf-8'); header('Content-Disposition: attachment; filename="rotulados.xls"'); header('Pragma: no-cache'); header('Expires: 0'); $output = ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $numero_orden = 1; foreach ($pedidos as $pedido) { $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $numero_orden++; } $output .= ''; $output .= '
DNIClienteSede de EnvíoNumeroProductoCantidadNumero de Orden
' . htmlspecialchars($pedido['dni_cliente'] ?? '') . '' . htmlspecialchars($pedido['nombre_completo'] ?? '') . '' . htmlspecialchars($pedido['sede_envio'] ?? '') . '' . htmlspecialchars($pedido['celular'] ?? '') . '' . htmlspecialchars($pedido['producto'] ?? '') . '' . htmlspecialchars($pedido['cantidad'] ?? '') . '' . $numero_orden . '
'; $output .= ''; echo $output; } catch (Exception $e) { // Log error if possible, but don't output it to the user here // as it would corrupt the Excel file. // For debugging, you could write to a log file: // file_put_contents('error_log.txt', $e->getMessage()); // Restore friendly error message header('HTTP/1.1 500 Internal Server Error'); // Using a script to show the message and prevent file corruption. echo ''; } ?>