Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/12/2012, 14:53
dark_17
 
Fecha de Ingreso: noviembre-2012
Ubicación: Villa Maria, Peru
Mensajes: 46
Antigüedad: 11 años, 5 meses
Puntos: 0
Problemas al exportar a excel desde Mysql con PHP

Sere preciso, espero que me ayuden, quiero exportar datos de una consulta a excel, he visto que hay un manual para hacerlo en esta pagina per quiero amoldarlo a mi clase "conexion", pues no quiero tener mas de una clase para conectarse, bueno mi clase conexion es esta:
Código PHP:
<?php
class BaseDatos{
    
#--------------- constructor
    
public function __construct($t='mysql',$h='localhost',$u='root',$p='123',$b='dbhotel'){
        
$this->tipo=$t;
        
$this->host=$h;
        
$this->user=$u;
        
$this->pass=$p;
        
$this->base=$b;
    }
    
#--------------- conexion con base de datos
    
public function db_conectar(){
        switch (
$this->tipo){
            case 
'mysql':
                
$cnx mysql_connect($this->host,$this->user,$this->pass);
                if(
$cnx){
                    
mysql_select_db($this->base);
                }else{
                    die(
'error al conectar a BD');
                }
                break;
            case 
'postgres';
                break;
        }
    }
    
#--------------- consultando a bd, devuelve un array de "registros"
    
public function db_consultar($sql){
        switch(
$this->tipo){
            case 
'mysql':
                
$this->db_conectar();
                
$resultados=mysql_query($sql);
                while(
$registro=mysql_fetch_assoc($resultados)){
                    
$salida[]=$registro;
                }
                return 
$salida;
                break;
            case 
'postgres':
                break;
        }
    } 
    
#--------------- Ejecutando comandos, regresara un boleano
    
public function db_ejecutar($sql){
        switch(
$this->tipo){
            case 
'mysql':
                
$this->db_conectar();
                
$rpta mysql_query($sql);
                return 
$rpta;
                break;
            case 
'postgres':
                break;
        }
    }
}
?>
Y amolde su tutorial para que funque con mi clase y lo hice algo asi:

Código PHP:
<?php
/*
Mysql To Excel
Generación de excel versión 1.0
Nicolás Pardo - 2007
*/
#Conexion a la db
include_once('componentes/base_class.php');
 
$w = new BaseDatos;
$datos2 $w->db_consultar("SELECT 
concat( MONTHNAME( v.fecha_venta ) , ' ', YEAR( v.fecha_venta ) ) AS MES, 
v.fecha_venta, 
SUM( v.facturado ) AS facturado , 
SUM( v.costo_fijos ) AS costo_fijos , 
SUM( v.costo_variables ) AS costo_variables , 
SUM( v.costo_fijos + v.costo_variables ) AS 'TOTAL COSTES', 
SUM( v.facturado - ( v.costo_fijos + v.costo_variables ) ) AS PROFIT
FROM wp_venta v
INNER JOIN wp_tipoventa t ON v.wp_tipoventa_idtipo_venta = t.idtipo_venta
INNER JOIN wp_hotel h ON v.wp_hotel_iddepart = h.iddepart
WHERE v.estado_venta = '1'
AND MONTHNAME( v.fecha_venta ) = MONTHNAME( NOW( ) )
AND YEAR( v.fecha_venta ) = YEAR( NOW( ) )
GROUP BY MONTHNAME( v.fecha_venta )
ORDER BY v.id_venta DESC"
);

 if (
$datos2 == 0) {
                                                    } else if (
$datos2 0) {
                                                    foreach (
$datos2 as $info) {
                                       
?>
                                        
                                          <tr>
                                              <td align="center"><?php echo $info['tv_descripcion'?></td>
                                            <td align="center"><?php echo $info['facturado'?></td>            
                                            <td  align="center"><?php echo $info['costo_fijos'?></td>
                                            <td  align="center"><?php echo $info['costo_variables'?></td>    
                                            <td  align="center"><?php echo $info['TOTAL COSTES'?></td>    
                                            <td  align="center"><?php echo $info['PROFIT'?></td>                                                            
                                          </tr>
                                        
        <?php
    
}
}

#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=TOTAL_DEL_MES_".date('d-m-Y').".xlsx");
?>
Y me sale este error:


1630.00 30.00 90.00 120.00 1510.00
Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\HOTELLIMA4RENT\ctr_act_ventatipo.ph p:34) in C:\AppServ\www\HOTELLIMA4RENT\ctr_act_ventatipo.ph p on line 47

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\HOTELLIMA4RENT\ctr_act_ventatipo.ph p:34) in C:\AppServ\www\HOTELLIMA4RENT\ctr_act_ventatipo.ph p on line 48

Creo que me indica que tengo que cambiar algo de la cabecera pero tengo idea de que ni como :s

Ahh olvidaba las lineas 47 y 48 son estas:
header("Content-type: application/vnd-ms-excel; charset=iso-8859-1");
header("Content-Disposition: attachment; filename=TOTAL_DEL_MES_".date('d-m-Y').".xlsx");