Ver Mensaje Individual
  #10 (permalink)  
Antiguo 02/07/2009, 12:06
Avatar de colote
colote
 
Fecha de Ingreso: julio-2008
Ubicación: Rosario - Santa Fe - Argentina
Mensajes: 729
Antigüedad: 15 años, 10 meses
Puntos: 8
Respuesta: crear archivo XLS

Hola, por ahi te sirve de ayuda este script que saque del foro ....

Código php:
Ver original
  1. <?php
  2. /*Mysql To Excel
  3. Generación de excel versión 1.0
  4. Nicolás Pardo - 2007
  5. */
  6. #Conexion a la db
  7. require_once('conexion.php');
  8.  
  9. #Sql, acá pone tu consulta a la tabla que necesites exportar filtrando los datos que creas necesarios.
  10. $sql = "SELECT * FROM clientes ORDER BY razon_soc ASC";
  11.  
  12. $r = mysql_query( $sql ) or trigger_error( mysql_error($conn), E_USER_ERROR );
  13. $return = '';
  14. if( mysql_num_rows($r)>0){
  15.     $return .= '<table border=1>';
  16.     $cols = 0;
  17.     while($rs = mysql_fetch_row($r)){
  18.         $return .= '<tr>';
  19.         if($cols==0){
  20.             $cols = sizeof($rs);
  21.             $cols_names = array();
  22.             for($i=0; $i<$cols; $i++){
  23.                 $col_name = mysql_field_name($r,$i);
  24.                 $return .= '<th>'.htmlspecialchars($col_name).'</th>';
  25.                 $cols_names[$i] = $col_name;
  26.             }
  27.             $return .= '</tr><tr>';
  28.         }
  29.         for($i=0; $i<$cols; $i++){
  30.             #En esta iteración podes manejar de manera personalizada datos, por ejemplo:
  31.            if($cols_names[$i] == 'fechaAlta'){ #Fromateo el registro en formato Timestamp
  32.                $return .= '<td>'.htmlspecialchars(date('d/m/Y H:i:s',$rs[$i])).'</td>';
  33.             }else if($cols_names[$i] == 'activo'){ #Estado lógico del registro, en vez de 1 o 0 le muestro Si o No.
  34.                $return .= '<td>'.htmlspecialchars( $rs[$i]==1? 'SI':'NO' ).'</td>';
  35.             }else{
  36.                 $return .= '<td>'.htmlspecialchars($rs[$i]).'</td>';
  37.             }
  38.         }
  39.         $return .= '</tr>';
  40.     }
  41.     $return .= '</table>';
  42. }
  43. #Cambiando el content-type más las <table> se pueden exportar formatos como csv
  44. header("Content-type: application/vnd-ms-excel; charset=iso-8859-1");
  45. header("Content-Disposition: attachment; filename=NombreDelExcel_".date('d-m-Y').".xls");
  46. echo $return;  
  47. ?>

Saludos, Ricardo !!!
__________________
Mail: [email protected]