Ver Mensaje Individual
  #4 (permalink)  
Antiguo 10/01/2015, 09:30
Avatar de loncho_rojas
loncho_rojas
Colaborador
 
Fecha de Ingreso: octubre-2008
Ubicación: En el mejor lugar del mundo
Mensajes: 2.704
Antigüedad: 15 años, 6 meses
Puntos: 175
Respuesta: Definir el tamaño de las columnas al exportar datos a excel desde php

Mejor utiliza el PHPExcel como libreria...

con un setAutoSize se soluciona, te permite convertir campos numericos, seting, fecha etc...

Código PHP:
Ver original
  1. #Hacemos la consulta
  2.    $resultado = $classic->consulta($consulta); //Es un array, en la primera línea están los títulos        
  3.  
  4.     #Generamos el Excel
  5.    $PHPExcel = new PHPExcel();
  6.     $PHPExcel->getProperties()->setTitle("$nombre"); //Título del Excel
  7.  
  8.     $campo_excel = 1;
  9.     $letra = 'A';
  10.     $tipo_string = array();
  11.  
  12.     #Imprimo los títulos
  13.    foreach($resultado as $filas) {                
  14.         $columna_excel     = '0';
  15.         foreach($filas AS $titulo=>$fila) {            
  16.             #Las columnas todas en String            
  17.            $PHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($columna_excel, $campo_excel, $titulo, PHPExcel_Cell_DataType::TYPE_STRING);
  18.             #Si la columna la queremos en string la metemos en el array
  19.            if ($titulo=='codigo' || $titulo=='refprov' || $titulo=='CODIGO_' || $titulo=='ean' || $titulo=='CODIGO') {
  20.                 $tipo_string[] = $columna_excel; //Esta columna será tipo String
  21.             }
  22.             $columna_excel++;        
  23.             $letra++; //Para saber cuantas columnas tengo (en letra) y al final adelante usar un loop para autoajustar el texto      
  24.         }                
  25.         $campo_excel++;                    
  26.         break;
  27.     }
  28.  
  29.     function tipo_string($columna, $tipo_string) {
  30.         #Si la columna está en el array convertimos la celda a String                
  31.        if (empty($tipo_string)) return false;
  32.  
  33.         foreach($tipo_string AS $string) {            
  34.             if ($columna == $string)
  35.                 return true;            
  36.         }
  37.     }
  38.  
  39.     #Imprimo los campos
  40.    foreach($resultado AS $filas) {
  41.         $columna_excel = '0';
  42.         foreach($filas AS $fila) {            
  43.             if (tipo_string($columna_excel, $tipo_string)) {
  44.                 $PHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($columna_excel, $campo_excel, $fila, PHPExcel_Cell_DataType::TYPE_STRING);                            
  45.             }
  46.             else {
  47.                 $PHPExcel->getActiveSheet()->SetCellValueByColumnAndRow($columna_excel, $campo_excel, $fila);                                                        
  48.             }
  49.  
  50.             $columna_excel++;    
  51.         }
  52.         $campo_excel++;
  53.     }
  54.  
  55.     #Ajusto ancho de las columnas al texto
  56.    for ($col = 'A'; $col != $letra; $col++) {
  57.         $PHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);        
  58.     }
  59.  
  60.     $fecha = DATE('d-m-Y H.i');
  61.      
  62.     //Excel 2007
  63.     /*
  64.     header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  65.     header('Content-Disposition: attachment;filename="'.$fecha.'_informe_'.$nombre.'.xlsx"');
  66.     header('Cache-Control: max-age=0');
  67.     $PHPExcel = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007');
  68.     */
  69.  
  70.     //Excel 2003
  71.     header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  72.     header('Content-Disposition: attachment;filename="'.$fecha.'_informe_'.$nombre.'.xls"');
  73.     header('Cache-Control: max-age=0');
  74.     $PHPExcel = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel5');
  75.      
  76.     //Creamos el Archivo    
  77.     $PHPExcel->save('php://output');
  78. ?>

FUENTE
__________________
Ayudo con lo que puedo en el foro, y solo en el foro.. NO MENSAJES PRIVADOS.. NO EMAILS NI SKYPE u OTROS.

Antes de hacer un TOPICO piensa si puedes hallarlo en Google o en el Buscador del Foro...