Foros del Web » Programando para Internet » PHP »

problema al exportar una consulta hecha en php a excel

Estas en el tema de problema al exportar una consulta hecha en php a excel en el foro de PHP en Foros del Web. Hola amigos foreros espero ustedes puedan ayudarme con la siguiente duda: tome como base un script que estaba publicado en FDW para poder exportar el ...
  #1 (permalink)  
Antiguo 14/05/2007, 12:43
Avatar de ing_aprendiz  
Fecha de Ingreso: septiembre-2005
Mensajes: 136
Antigüedad: 18 años, 7 meses
Puntos: 0
problema al exportar una consulta hecha en php a excel

Hola amigos foreros espero ustedes puedan ayudarme con la siguiente duda: tome como base un script que estaba publicado en FDW para poder exportar el resultado de una consulta mysql hecha en una pagina php a un archivo de excel lo que estoy haciendo es lo siguiente:

Código PHP:
define(db_host"localhost");
define(db_user"root");
define(db_pass"");
define(db_linkmysql_connect(db_host,db_user,db_pass));
define(db_name"productiva_contactos");
mysql_select_db(db_name);

$categoria $_GET['categoria'];
$subcategoria $_GET['subcategoria'];
$especialidad $_GET['especialidad'];
$IDDEPARTAMENTO $_GET['IDDEPARTAMENTO'];
$IDSERVICIO $_GET['IDSERVICIO'];
$ciudad $_GET['ciudad'];

/********************************************
Write the query, call it, and find the number of fields
/********************************************/
$select =("SELECT DISTINCT E.NOMBRE, S.DIRECCION, S.CIUDAD, C.NOMBRE, D.DESCRIPCION 
      FROM empresa E, sede S, contacto C, servicio_empresa P, tiposervicio T, departamento D
      WHERE S.IDEMPRESA = E.IDEMPRESA
      AND  S.IdSede = C.IdSede
      AND  S.IdSede = P.IdSede
      AND  S.IdEmpresa=P.IdEmpresa
      AND  P.IdServicio = T.IdServicio
      AND S.IDSEDE = C.IDCONTACTO
      AND C.IDDEPARTAMENTO = D.IDDEPARTAMENTO
      AND S.del ='1'
      AND E.del ='1'
      AND P.del ='1'
      AND C.del ='1' 
      AND ( E.CATEGORIA = '$categoria'
      OR E.SUBCATEGORIA = '$subcategoria'
      OR E.ESPECIALIDAD = '$especialidad'
      OR T.IDSERVICIO = '$IDSERVICIO'
      OR S.CIUDAD = '$ciudad'
      OR C.IDDEPARTAMENTO = '$IDDEPARTAMENTO')"
);
      
$export mysql_query($select);
$count mysql_num_fields($export);

/********************************************
Extract field names and write them to the $header
variable
/********************************************/
for ($i 0$i $count$i++) {
$header .= mysql_field_name($export$i)."\t";
}
/********************************************
Extract all data, format it, and assign to the $data
variable
/********************************************/
while($row mysql_fetch_row($export)) {
$line '';
foreach(
$row as $value) {
if ((!isset(
$value)) OR ($value == "")) {
$value "\t";
} else {
$value str_replace('"''""'$value);
$value '"' $value '"' "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data str_replace("\r"""$data);
/********************************************
Set the default message for zero records
/********************************************/
if ($data == "") {
$data "\n(0) Records Found!\n";
}
/********************************************
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");
print 
"$header\n$data"
resulta que si me genera el archivo en excel pero toda la información me la deja en una sola celda, la idea es que me salga el resultado de esta consulta como una tabla pero en excel

si alguno de ustedes puede ayudarme le agradecería

chao
ing_aprendiz
  #2 (permalink)  
Antiguo 15/05/2007, 14:51
Avatar de claudiovega  
Fecha de Ingreso: octubre-2003
Ubicación: Puerto Montt
Mensajes: 3.667
Antigüedad: 20 años, 6 meses
Puntos: 11
Re: problema al exportar una consulta hecha en php a excel

En las FAQ del foro hay un código que en lugar de generar saltos de línea genera html, lo cual el excel entiende perfectamente si lo grabas con extensión xls.
  #3 (permalink)  
Antiguo 16/05/2007, 15:16
Avatar de ing_aprendiz  
Fecha de Ingreso: septiembre-2005
Mensajes: 136
Antigüedad: 18 años, 7 meses
Puntos: 0
Re: problema al exportar una consulta hecha en php a excel

Hola gracias por costentar, pero ya le encontre la solución, de la siguiente manera:

Código PHP:
define(db_host"localhost");
define(db_user"root");
define(db_pass"");
define(db_linkmysql_connect(db_host,db_user,db_pass));
define(db_name"productiva_contactos");
mysql_select_db(db_name);


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

$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>Nombre Empresa</strong></font></td>
  <td><font color=\"#ffffff\"><strong>Dirección</strong></font></td>
  <TD><font color=\"#ffffff\"><strong>Ciudad</strong></font></TD>
  <td><font color=\"#ffffff\"><strong>Nombre contacto</strong></font></td>
  <td><font color=\"#ffffff\"><strong>Departamento</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
/********************************************/
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=consulta.xls");
header("Pragma: no-cache");
header("Expires: 0"); 
ing_aprendiz
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 19:20.