26/04/2011, 11:25
|
| | | Fecha de Ingreso: septiembre-2005 Ubicación: Cancún, México
Mensajes: 231
Antigüedad: 18 años, 7 meses Puntos: 28 | |
Respuesta: php, mysql a excel Hola!
Yo utilizo este codigo: Código PHP: <?php /* Mysql To Excel Generación de excel versión 1.0 Nicolás Pardo - 2007 */ #Conexion a la db require("../connect.inc"); #Sql, acá pone tu consulta a la tabla que necesites exportar filtrando los datos que creas necesarios. $sql = " SELECT * FROM xxxx ORDER BY xxxx DESC "; //$r = mysql_query( $sql ) or trigger_error( mysql_error($conn), E_USER_ERROR ); $query="select * from xxx order by xxxx ASC"; $connection = conect(); $r = mysql_query($query, $connection) or die(mysql_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=Registro_de_Usuarios_".date('d-m-Y').".xls"); echo $return; ?> Espero que te sirva....
Saludos,
__________________ Atentamente,
Pablo Ayala.
Si vas a responder algo en el foro que sea con amabilidad. |