Foros del Web » Programando para Internet » PHP »

problemas al exportar a excel

Estas en el tema de problemas al exportar a excel en el foro de PHP en Foros del Web. Lectores del foro tengo el siguiente problema , tengo un buscador en el cual el criterio de busqueda es la fecha , el problema es ...
  #1 (permalink)  
Antiguo 13/09/2011, 12:02
 
Fecha de Ingreso: enero-2011
Ubicación: /root
Mensajes: 530
Antigüedad: 13 años, 3 meses
Puntos: 61
problemas al exportar a excel

Lectores del foro tengo el siguiente problema , tengo un buscador en el cual el criterio de busqueda es la fecha , el problema es que intento implementar que los resultados que me muestre el buscador sean exportados a un formato excel , mi duda es que el parametro al archivo exportar excel lo paso bajo el parametro $GET pero no me sale ningun resultado, expongo el codigo espero puedan brindarme su ayuda .

buscar.php
Código PHP:
Ver original
  1. <?php
  2. if ($_POST['buscador']){
  3. $buscar = $_POST['palabra'];
  4. // Si está vacío
  5. if(empty($buscar)){
  6.     echo "No se han ingresado datos a Buscar";
  7. }
  8.  
  9. else{
  10.     $con=mysql_connect("localhost","xxx","xxx");
  11.     $sql = "SELECT
  12.                 unifica_proyectos.id,
  13.                 unifica_proyectos.nombreencuesta,
  14.                 unifica_proyectos.fecha,
  15.                 unifica_proyectos.rut,
  16.                 unifica_proyectos.nombreevaluador,
  17.                 unifica_proyectos.idregion,
  18.                 unifica_proyectos.coordinador,
  19.                 unifica_proyectos.mail,
  20.                 regiones.region,
  21.                 regiones.idregion
  22.                
  23.             FROM unifica_proyectos
  24.             INNER JOIN regiones ON unifica_proyectos.idregion = regiones.idregion WHERE fecha like '%$buscar%' ORDER BY id DESC";
  25.            
  26.     mysql_select_db("bd_mistery", $con);
  27.  
  28.     $result = mysql_query($sql, $con);
  29.     $total = mysql_num_rows($result);
  30.         if ($row = mysql_fetch_array($result)){
  31.             do {   
  32.                     $nombreencuesta=utf8_encode($row['nombreencuesta']);
  33.                     $fecha=$row['fecha'];  
  34.                     $fecha=date("d-m-Y",strtotime($fecha));
  35.                     $rut=$row['rut'];
  36.                     $nombreevaluador=utf8_encode($row['nombreevaluador']);
  37.                     $region=utf8_encode($row['region']);
  38.                     $coordinador=utf8_encode($row['coordinador']);
  39.                     $mail=utf8_encode($row['mail']);
  40.  
  41.                 echo "<table class=cambia-color>";
  42.                 echo "<tr>";
  43.                 echo "<td width=20%>$nombreencuesta</td>";
  44.                 echo "<td width=10%>$fecha</td>";
  45.                 echo "<td width=10%>$rut</td>";
  46.                 echo "<td width=20%>$nombreevaluador</td>";
  47.                 echo "<td width=10%>$region</td>";
  48.                 echo "<td width=10%>$coordinador</td>";
  49.                 echo "<td width=10%>$mail</td>";
  50.                 echo "</table>";
  51.                
  52.                
  53.                 }
  54.  
  55.     while ($row = mysql_fetch_array($result));
  56.  
  57.         }
  58. else {
  59. echo "No se encontraron resultados para: <b>$buscar</b>";
  60. }
  61. }
  62. }
  63. ?>
  64. <a href="exportar_excel.php?<?php echo $buscar;?>">Exportar</a>


exportar_excel.php

Código PHP:
Ver original
  1. $buscar=$_GET['palabra'];
  2.  
  3. header('Pragma: public');
  4. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past    
  5. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  6. header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
  7. header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
  8. header('Pragma: no-cache');
  9. header('Expires: 0');
  10. header('Content-Transfer-Encoding: none');
  11. header('Content-Type: application/vnd.ms-excel'); // This should work for IE & Opera
  12. header('Content-type: application/x-msexcel'); // This should work for the rest
  13. header('Content-Disposition: attachment; filename="nombre.xls"');
  14.  
  15. $conexion=mysql_connect("localhost","xxx","xxx");
  16.     $sql = "SELECT
  17.                 unifica_proyectos.id,
  18.                 unifica_proyectos.nombreencuesta,
  19.                 unifica_proyectos.fecha,
  20.                 unifica_proyectos.rut,
  21.                 unifica_proyectos.nombreevaluador,
  22.                 unifica_proyectos.idregion,
  23.                 unifica_proyectos.coordinador,
  24.                 unifica_proyectos.mail,
  25.                 regiones.region,
  26.                 regiones.idregion
  27.                
  28.             FROM unifica_proyectos
  29.             INNER JOIN regiones ON unifica_proyectos.idregion = regiones.idregion WHERE fecha='".$buscar."'";
  30.            
  31.     mysql_select_db("bd_mistery", $conexion);
  32.  
  33.     $result = mysql_query($sql, $conexion);
  34.     while($row = mysql_fetch_array($result)){
  35.        
  36.         $nombreencuesta=utf8_encode($row['nombreencuesta']);
  37.         $fecha=$row['fecha'];  
  38.         $fecha=date("d-m-Y",strtotime($fecha));
  39.         $rut=$row['rut'];
  40.         $nombreevaluador=utf8_encode($row['nombreevaluador']);
  41.         $region=utf8_encode($row['region']);
  42.         $coordinador=utf8_encode($row['coordinador']);
  43.         $mail=utf8_encode($row['mail']);
  44.            
  45. echo '<table xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
  46.   <tr>
  47.       <td>$nombreencuesta</td>
  48.        <td>$fecha</td>
  49.        <td>$rut</td>
  50.        <td>$nombreevaluador</td>
  51.        <td>$region</td>
  52.        <td>$coordinador</td>
  53.        <td>$mail</td>
  54.   </tr>
  55. </table>';
  56. }

saludos y de antemano gracias
  #2 (permalink)  
Antiguo 13/09/2011, 16:31
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: problemas al exportar a excel

Si pruebas esa misma consulta en PhpMyAdmin, te devuelve resultados?
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 14/09/2011, 09:00
 
Fecha de Ingreso: enero-2011
Ubicación: /root
Mensajes: 530
Antigüedad: 13 años, 3 meses
Puntos: 61
Respuesta: problemas al exportar a excel

mi amigo sabes la consulta esta bien por ej al ejecutar algo como esto ,

Código MySQL:
Ver original
  1. unifica_proyectos.id,
  2. unifica_proyectos.nombreencuesta,
  3. unifica_proyectos.fecha,
  4. unifica_proyectos.rut,
  5. unifica_proyectos.nombreevaluador,
  6. unifica_proyectos.idregion,
  7. unifica_proyectos.coordinador,
  8. unifica_proyectos.mail,
  9. regiones.idregion,
  10. regiones.region
  11. unifica_proyectos
  12. INNER JOIN regiones ON unifica_proyectos.idregion = regiones.idregion
  13. fecha = '2011-09-01 15:14:13'

me muestra resultados , el campo esta como datetime , incluso la primera consulta

Código MySQL:
Ver original
  1.                 unifica_proyectos.id,
  2.                 unifica_proyectos.nombreencuesta,
  3.                 unifica_proyectos.fecha,
  4.                 unifica_proyectos.rut,
  5.                 unifica_proyectos.nombreevaluador,
  6.                 unifica_proyectos.idregion,
  7.                 unifica_proyectos.coordinador,
  8.                 unifica_proyectos.mail,
  9.                 regiones.region,
  10.                 regiones.idregion
  11.                
  12.             FROM unifica_proyectos
  13.             INNER JOIN regiones ON unifica_proyectos.idregion = regiones.idregion WHERE fecha like '%$buscar%' ORDER BY id DESC"

al cambiarla por
WHERE unifica_proyectos.fecha='".$buscar."' y quitarle el like no me muestra nada , espero alguien pueda ayudarme .

saludos y gracias
  #4 (permalink)  
Antiguo 14/09/2011, 12:14
 
Fecha de Ingreso: enero-2011
Ubicación: /root
Mensajes: 530
Antigüedad: 13 años, 3 meses
Puntos: 61
Respuesta: problemas al exportar a excel

edito , resolvi mi problema eran las comillas dejo el script por si alguien tiene el mismo problema .

Código PHP:
Ver original
  1. <?php
  2. require_once("../conexion/conexion.php");
  3.  
  4. $buscar=$_GET['palabra'];
  5.  
  6. header('Pragma: public');
  7. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past    
  8. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  9. header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
  10. header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
  11. header('Pragma: no-cache');
  12. header('Expires: 0');
  13. header('Content-Transfer-Encoding: none');
  14. header('Content-Type: application/vnd.ms-excel'); // This should work for IE & Opera
  15. header('Content-type: application/x-msexcel'); // This should work for the rest
  16. header('Content-Disposition: attachment; filename="Informe.xls"'); 
  17.  
  18.  
  19.     $sql = "SELECT unifica_proyectos.id,
  20.                    unifica_proyectos.nombreencuesta,
  21.                   unifica_proyectos.fecha,
  22.                    unifica_proyectos.rut,
  23.                    unifica_proyectos.nombreevaluador,
  24.                    unifica_proyectos.idregion,
  25.                    unifica_proyectos.coordinador,
  26.                    unifica_proyectos.mail,regiones.idregion,
  27.                   regiones.region
  28.             FROM unifica_proyectos INNER JOIN regiones ON unifica_proyectos.idregion = regiones.idregion
  29.             WHERE unifica_proyectos.fecha ='".$buscar."'";
  30.  
  31.     $resultado=mysql_query($sql,$link)or die(mysql_error());
  32.         while($row = mysql_fetch_array($resultado)) {
  33.        
  34.         $nombreencuesta=utf8_encode($row['nombreencuesta']);
  35.         $fecha=$row['fecha'];  
  36.         $fecha=date("d-m-Y",strtotime($fecha));
  37.         $rut=$row['rut'];
  38.         $nombreevaluador=utf8_encode($row['nombreevaluador']);
  39.         $region=utf8_encode($row['region']);
  40.         $coordinador=utf8_encode($row['coordinador']);
  41.         $mail=utf8_encode($row['mail']);
  42.            
  43.        
  44. echo "<table xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>
  45.   <tr>
  46.       <td>$nombreencuesta</td>
  47.        <td>$fecha</td>
  48.        <td>$rut</td>
  49.        <td>$nombreevaluador</td>
  50.        <td>$region</td>
  51.        <td>$coordinador</td>
  52.        <td>$mail</td>
  53.   </tr>
  54. </table>";
  55. }
  56. ?>

saludos

Última edición por matt_1985; 14/09/2011 a las 12:27

Etiquetas: excel, html, mysql, sql
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 09:30.