Ver Mensaje Individual
  #1 (permalink)  
Antiguo 03/04/2011, 00:09
razer
 
Fecha de Ingreso: marzo-2008
Mensajes: 237
Antigüedad: 16 años, 1 mes
Puntos: 6
Crear XLS con php

Buenas, tengo un problema.
Estoy tratando de generar un xls con PHP, y sé como hacerlo, pero el header me trae problemas. Es decir:

Si pongo un table con columnas, filas, etc, todo común funciona, el problema es cuando hago includes, querys, y otras cosas que no sean formato html / para escribir en excel.

Esto es algo muy comun que si funciona
Código:
<?php 
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=reporte.xls");
?>
<html>
<head>
<title>Excel</title>
</head>
<body>
<table border="1">
<tr>
<td>
Hola mundo
</td>
</tr>
</table>
</body>
</html>
El problema es cuando quiero usar PHP con funciones, includes, etc.
Muestro el codigo:
Código:
<?
 header("Content-type: application/vnd.ms-excel");
 header("Content-Disposition: attachment; filename=listaPeliculas.xls");
 
 include 'add/conexion.php'; 
 include 'add/funcionesVarias.php';
 
 $query = "SELECT id, nombre, genero, ano, detalles FROM peliculas WHERE addhome=1 ORDER BY id DESC";
 $conexion = conectar();
 $result = mysql_query($query, $conexion) or die("ERROR: Problemas en la DB. No se pudo hacer el select: " . mysql_error()); 
 desconectar();

 $i=0;
 while($pelicula = mysql_fetch_array($result))
 {
  $id[$i] = $pelicula['id'];
  $nombre[$i] = $pelicula['nombre'];
  $genero[$i] = $pelicula['genero'];
  $ano[$i] = $pelicula['ano'];
  $detalle[$i] = $pelicula['detalles'];
  $i++;
 }
 
 //GENERACION EXCEL-------------------------------------------------------------------------
 //Enviamos los encabezados de hoja de calculo
 //Creamos la tabla
 echo "<table>";
 for($q=0; $q < 5; $q++) //5 columnas
 {
  if($q == 0) echo "<tr><td>ID</td>";
  else if($q == 1) echo "<tr><td>NOMBRE</td>";
  else if($q == 2) echo "<tr><td>GENERO</b></td>";
  else if($q == 3) echo "<tr><td>AÑO</td>";
  else if($q == 4) echo "<tr><td>DETALLE</td>";
    
  for($j=0; $j <= $i; $j++)
  {
   if($q == 0) echo "<td>" . $id[$q][$j] . "</td>";
   else if($q == 1) echo "<td>" . $nombre[$q][$j] . "</td>";
   else if($q == 2) echo "<td>" . $genero[$q][$j] . "</td>";
   else if($q == 3) echo "<td>" . $ano[$q][$j] . "</td>";
   else if($q == 4) echo "<td>" . $detalle[$q][$j] . "</td>";
  }
  
  echo "</tr>";
 }
 echo "</table>";
?>
No funciona por culpa del header, porque seguramente a continuación del header iria exclusivamente la data del excel y nada mas. Pero yo necesito traer data de base de datos para escribirla en excel. Entonces, como hacer?

Slds.