Ver Mensaje Individual
  #2 (permalink)  
Antiguo 03/01/2013, 04:17
alyciashape
 
Fecha de Ingreso: abril-2012
Mensajes: 590
Antigüedad: 12 años
Puntos: 58
Respuesta: Formato columna String en PHPExcel

Bueno ya lo he solucionado. Dejo el código por si le ayuda a alguien.

Código PHP:
    #Hacemos la consulta
    
$resultado $classic->consulta($consulta); //Es un array, en la primera línea están los títulos        

    #Generamos el Excel
    
$PHPExcel = new PHPExcel();
    
$PHPExcel->getProperties()->setTitle("$nombre"); //Título del Excel

    
$campo_excel 1;
    
$letra 'A';
    
$tipo_string = array();

    
#Imprimo los títulos
    
foreach($resultado as $filas) {                
        
$columna_excel     '0';
        foreach(
$filas AS $titulo=>$fila) {            
            
#Las columnas todas en String            
            
$PHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($columna_excel$campo_excel$tituloPHPExcel_Cell_DataType::TYPE_STRING);
            
#Si la columna la queremos en string la metemos en el array
            
if ($titulo=='codigo' || $titulo=='refprov' || $titulo=='CODIGO_' || $titulo=='ean' || $titulo=='CODIGO') {
                
$tipo_string[] = $columna_excel//Esta columna será tipo String
            
}
            
$columna_excel++;        
            
$letra++; //Para saber cuantas columnas tengo (en letra) y al final adelante usar un loop para autoajustar el texto     
        
}                
        
$campo_excel++;                    
        break;
    }

    function 
tipo_string($columna$tipo_string) {
        
#Si la columna está en el array convertimos la celda a String                
        
if (empty($tipo_string)) return false;

        foreach(
$tipo_string AS $string) {            
            if (
$columna == $string)
                return 
true;            
        }
    }

    
#Imprimo los campos
    
foreach($resultado AS $filas) {
        
$columna_excel '0';
        foreach(
$filas AS $fila) {            
            if (
tipo_string($columna_excel$tipo_string)) {
                
$PHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($columna_excel$campo_excel$filaPHPExcel_Cell_DataType::TYPE_STRING);                            
            }
            else {
                
$PHPExcel->getActiveSheet()->SetCellValueByColumnAndRow($columna_excel$campo_excel$fila);                                                        
            }

            
$columna_excel++;    
        }
        
$campo_excel++;
    }

    
#Ajusto ancho de las columnas al texto
    
for ($col 'A'$col != $letra$col++) {
        
$PHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);        
    }

    
$fecha DATE('d-m-Y H.i');
    
    
//Excel 2007
    /*
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$fecha.'_informe_'.$nombre.'.xlsx"');
    header('Cache-Control: max-age=0');
    $PHPExcel = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007');
    */

    //Excel 2003
    
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    
header('Content-Disposition: attachment;filename="'.$fecha.'_informe_'.$nombre.'.xls"');
    
header('Cache-Control: max-age=0');
    
$PHPExcel PHPExcel_IOFactory::createWriter($PHPExcel'Excel5');
     
    
//Creamos el Archivo    
    
$PHPExcel->save('php://output');
?> 
Hay cosas mias limpié lo que pude no me puedo parar más. Si no se entiende algo preguntar lo que sea.