New question

Question:

Date: 10-06-2019 23:28:42 (In Spanish)

Exportar registros a excel[Unresolved]

Hola que tal.

Tengo una tabla en la cual almaceno registros, dentro de esa página cuento con un botón para realizar búsquedas en la tabla. Una de las funciones del botón Buscar tiene que debe mostrarme todos los registros que pertenezcan a la misma zona, es decir, NORTE, CENTRO Y SUR.

Sí yo escribo dentro del input de buscar: "NORTE" y presiono buscar, me muestra todos los registros que pertenecen a la zona norte.

Mi pregunta es: Cómo le hago para exportar a excel sólo esos registros (NORTE, CENTRO, SUR) dependiendo de los que se buscó?

Aquí el código que estoy utilizando para exportar a excel:

<?php
include 'cn.php';
$output = '';
if(isset($_POST["export_excel"]))
{
$sql = "SELECT * FROM fallas WHERE estatus = 1 ORDER BY id ASC";
$result = mysqli_query($conexion, $sql);
if(mysqli_num_rows($result)>0)
{
$output .= '
<table class="table" border="1">
<tr>
<h1>FALLAS </h1>
<th>ID</th>
<th>FECHA DE FALLA</th>
<th>ZONA</th>
<th>UDEN</th>
<th>NOMBRE</th>
<th>DISPOSITIVO EN FALLA</th>
<th>FALLA</th>
<th>TÉCNICO</th>
<th>FECHA DE SOLUCIÓN</th>
<th>OBSERVACIONES</th>
<th>ESTATUS</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .='
<tr>
<td>'.$row["id"].'</td>
<td>'.$row["fecha_falla"].'</td>
<td>'.$row["zona"].'</td>
<td>'.$row["uden"].'</td>
<td>'.$row["nombre"]. '</td>
<td>'.$row["dispositivo"]. '</td>
<td>'.$row["falla"].'</td>
<td>'.$row["tecnico"].'</td>
<td>'.$row["fecha_solucion"].'</td>
<td>'.$row["observaciones"].'</td>
<td>'.$row["estatus"].'</td>
</tr>
';

}
$output .='</table>';
header("Content-Type: application/xls");
header("Content-Disposition:attachment, filename=download.xls");
echo $output;
}
}
?>
Tags: HTML - MySQL - PHP Votes: 0 - Answers: 6 - Views: 19 Share on: Google Facebook Twitter LinkedIn Link
 

Answers:

  • Date: 12-06-2019 06:37:09 Hola..
    No trates de reinventar la rueda... y menos con algo tan complejo como es exportar a excel.
    Te recomiendo la librería: PhpSpreadsheet antes conocida como PHPExcel, el cual es muy fácil crear un excel con información desde un vector (Arreglo de arreglos), ya que al exportarlos así como lo pones te puede dar error al abrirlo...

    En la documentación vienen muchos ejemplos, y al exportar puedes agregar, imágenes (logo de la empresa) y muchas cosas mas, para hacer mas profesional tu resultados.

    Saludos terricola.

    Stryfe™
      Votes: 1 - Link answer
     
  • Date: 13-06-2019 05:42:17 Concuerdo con Ernesto. Te dejo este artículo que escribí donde tienes más detalles de la implementación: https://academy.leewayweb.com/como-interactuar-con-excel-desde-php/   Votes: 0 - Link answer
     
  • Date: 13-06-2019 06:11:59 Saludos Mauro, paa comentarte que tu link de curso de PHP Orientado a Objetos que tienes en ese articulo no se ve...
    Saludos Terricolas.

    Stryfe™
      Votes: 0 - Link answer
     
  • Date: 13-06-2019 10:53:09 Buenísimo. Gracias!   Votes: 0 - Link answer
     
  • Date: 14-06-2019 08:23:44 Usar el formato de texto CSV que es mucho más sencillo y compatible.

    Saludos.
      Votes: 0 - Link answer
     
  • Date: 17-06-2019 08:12:44 yo lo hice así...

    <?php
    include_once('conexion.php');

    $sql = " SELECT << campos >>
    FROM << bdd >>
    WHERE << condiciones >>
    AND << condicion >> ";

    $r = mysql_query( $sql ) or trigger_error( mysql_error($conn), E_USER_ERROR );
    $return = '';
    if( mysql_num_rows($r)>0)
    {
    $return .= '<table border=1>';
    $cols = 0;
    while($rs = mysql_fetch_row($r)){
    $return .= '<tr>';
    if($cols==0){
    $cols = sizeof($rs);
    $cols_names = array();
    for($i=0; $i<$cols; $i++){
    $col_name = mysql_field_name($r,$i);
    $return .= '<th>'.htmlspecialchars($col_name).'</th>';
    $cols_names[$i] = $col_name;
    }
    $return .= '</tr><tr>';
    }
    for($i=0; $i<$cols; $i++){
    #En esta iteración podes manejar de manera personalizada datos, por ejemplo:
    if($cols_names[$i] == 'fechaAlta'){ #Fromateo el registro en formato Timestamp
    $return .= '<td>'.htmlspecialchars(date('d/m/Y H:i:s',$rs[$i])).'</td>';
    }else if($cols_names[$i] == 'activo'){ #Estado lógico del registro, en vez de 1 o 0 le muestro Si o No.
    $return .= '<td>'.htmlspecialchars( $rs[$i]==1? 'SI':'NO' ).'</td>';
    }else{
    $return .= '<td>'.htmlspecialchars($rs[$i]).'</td>';
    }
    }
    $return .= '</tr>';
    }
    $return .= '</table>';
    mysql_free_result($r);
    }
    #Cambiando el content-type más las <table> se pueden exportar formatos como csv
    header("Content-type: application/vnd-ms-excel; charset=iso-8859-1");
    header("Content-Disposition: attachment; filename=Rastros_CT_Excel_".date('d-m-Y').".xls");
    echo $return;
    ?>
      Votes: 0 - Link answer
     
To actively participate in the community first must authenticate, enter the system.Sign In
 
frjcbbae garagebible.com