Foros del Web » Programando para Internet » PHP »

Generar excel con php

Estas en el tema de Generar excel con php en el foro de PHP en Foros del Web. Hola, he estado buscando en el foro, como siempre, y practicando, antes de preguntar, Estoy intentando crear un excel con una tabla que dibujo en ...
  #1 (permalink)  
Antiguo 20/03/2008, 21:27
Avatar de jaronu  
Fecha de Ingreso: febrero-2008
Mensajes: 2.183
Antigüedad: 16 años, 1 mes
Puntos: 52
Generar excel con php

Hola,

he estado buscando en el foro, como siempre, y practicando, antes de preguntar,

Estoy intentando crear un excel con una tabla que dibujo en pantalla con una consulta a BD, hasta aqui bien, lo que pasa es que me dan error todos los headers y ya se que los headers no pueden enviarse si antes hay html pero es que en el el post que lei, recomendado por carlojas decia que funcionaba.

Código PHP:


<?php define(db_host"host"); 
define(db_user"r"); 
define(db_pass""); 
define(db_linkmysql_connect(db_host,db_user,db_pass)); 
define(db_name"r"); 
mysql_select_db(db_name); 


/******************************************** 
Write the query, call it, and find the number of fields 
/********************************************/ 
$qry =mysql_query("SELECT * from descargas"); 

$campos mysql_num_fields($qry);   
$i=0;   

/******************************************** 
Extract field names and write them to the $header 
variable 
/********************************************/ 
echo "&nbsp;<center><table border=\"1\" align=\"center\">"
echo 
"<tr bgcolor=\"#336666\"> 
  <td><font color=\"#ffffff\"><strong>ID</strong></font></td> 
  <td><font color=\"#ffffff\"><strong>NOMBRE</strong></font></td> 
  <TD><font color=\"#ffffff\"><strong>DESCRIPCION</strong></font></TD> 
  <td><font color=\"#ffffff\"><strong>RUTA</strong></font></td> 
  <td><font color=\"#ffffff\"><strong>TIPO</strong></font></td> 
  <td><font color=\"#ffffff\"><strong>TAMAÑO</strong></font></td>
  <td><font color=\"#ffffff\"><strong>CATEGORIA</strong></font></td>
</tr>"

while(
$row=mysql_fetch_array($qry)) 
{   
    echo 
"<tr>";   
     for(
$j=0$j<$campos$j++) {   
         echo 
"<td>".$row[$j]."</td>";   
     }   
     echo 
"</tr>";         
}   
echo 
"</table>"


/******************************************** 
Set the automatic downloadn section 
/********************************************/
 
?>
<?php 
header
("Content-type: application/vnd.ms-excel"); 
header("Content-Disposition: attachment; filename=consulta.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0");  

@
readfile$filename );
se genera muy bien la tabla, pero si pongo los headers en otra pagina php
¿que variable deberia pasarle a esa pagina para que cogiera toda la tabla que genero.

Si alguien sabe del tema agradeceria su ayuda.

Un saludo.
  #2 (permalink)  
Antiguo 20/03/2008, 23:09
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 10 meses
Puntos: 2135
Re: Generar excel con php

"Podrías" hacerlo así:
Código PHP:
<?php define(db_host"host"); 
define(db_user"r"); 
define(db_pass""); 
define(db_linkmysql_connect(db_host,db_user,db_pass)); 
define(db_name"r"); 
mysql_select_db(db_name); 


/******************************************** 
Write the query, call it, and find the number of fields 
/********************************************/ 
$qry =mysql_query("SELECT * from descargas"); 

$campos mysql_num_fields($qry);   
$i=0;   

/******************************************** 
Extract field names and write them to the $header 
variable 
/********************************************/
ob_start(); 
echo 
"&nbsp;<center><table border=\"1\" align=\"center\">"
echo 
"<tr bgcolor=\"#336666\"> 
  <td><font color=\"#ffffff\"><strong>ID</strong></font></td> 
  <td><font color=\"#ffffff\"><strong>NOMBRE</strong></font></td> 
  <TD><font color=\"#ffffff\"><strong>DESCRIPCION</strong></font></TD> 
  <td><font color=\"#ffffff\"><strong>RUTA</strong></font></td> 
  <td><font color=\"#ffffff\"><strong>TIPO</strong></font></td> 
  <td><font color=\"#ffffff\"><strong>TAMAÑO</strong></font></td>
  <td><font color=\"#ffffff\"><strong>CATEGORIA</strong></font></td>
</tr>"

while(
$row=mysql_fetch_array($qry)) 
{   
    echo 
"<tr>";   
     for(
$j=0$j<$campos$j++) {   
         echo 
"<td>".$row[$j]."</td>";   
     }   
     echo 
"</tr>";         
}   
echo 
"</table>"

$reporte ob_get_clean();
/******************************************** 
Set the automatic downloadn section 
/********************************************/

header("Content-type: application/vnd.ms-excel"); 
header("Content-Disposition: attachment; filename=consulta.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0");  

echo 
$reporte;
Saludos.
  #3 (permalink)  
Antiguo 20/03/2008, 23:25
Avatar de jaronu  
Fecha de Ingreso: febrero-2008
Mensajes: 2.183
Antigüedad: 16 años, 1 mes
Puntos: 52
Re: Generar excel con php

Muchisimas gracias como siempre GatorV.

Ha funcionado perfecto,

a me estaba liando con una clase que encontre tambien en el foro pero fallama demasiado.

es que el php es un mundo de funciones, por que estas son las que han hecho que no me den error los headers??

ob_start(); y $reporte = ob_get_clean();

Gracias.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:38.