todos texto phpspreadsheet los justificar formato condicional columns columna centrar celda bordes ancho all ajustar php width phpexcel autosize

texto - phpspreadsheet autosize all columns



Cómo PHPExcel establecer ancho de columnas automáticas (2)

Estoy trabajando con PHPExcel para exportar datos para descargar. Cuando se abren archivos descargados, las celdas tienen un número grande, muestra "#######" en lugar de un número de valor. Intenté setAutoSize() para cada columna, luego llame a $sheet->calculateColumnWidths() pero todavía no cambia. Veo calcularColumnWidths () aquí , @Mark Baker dice que "calculaColumnWidths () aumenta el valor en quizás un 5% para tratar de asegurar que toda la columna encaja". Si la longitud del número en la celda excede el 5%, parece que Doen resolvió el problema

ACTUALIZACIÓN Esta es mi función para auto tamaño de columnas:

function autoFitColumnWidthToContent($sheet, $fromCol, $toCol) { if (empty($toCol) ) {//not defined the last column, set it the max one $toCol = $sheet->getColumnDimension($sheet->getHighestColumn())->getColumnIndex(); } for($i = $fromCol; $i <= $toCol; $i++) { $sheet->getColumnDimension($i)->setAutoSize(true); } $sheet->calculateColumnWidths(); }


El primer problema potencial puede ser que estés trabajando con letras de columna. La operación incrementadora de PHP funcionará con letras de columna, por lo que si $ i es ''A'', $ i ++ dará ''B'', y si $ i es ''Z'' que $ i ++ dará ''AA''; pero no puede usar <= como su comparador, ya que ''AA'' es <= ''Z'' cuando se ejecuta como una comparación directa.

En lugar de

for($i = $fromCol; $i <= $toCol; $i++) {

utilizar

$toCol++; for($i = $fromCol; $i !== $toCol; $i++) {

Para agregar el margen del 5% después de llamar a $ sheet-> calculaColumnWidths () haga:

for($i = $fromCol; $i !== $toCol; $i++) { $calculatedWidth = $sheet->getColumnDimension($i)->getWidth(); $sheet->getColumnDimension($i)->setWidth((int) $calculatedWidth * 1.05); }


Ninguna de las sugerencias funcionó para mí, así que hice un cálculo manual (bastante fácil y rápido) (código de ejemplo a continuación) y funciona perfectamente (las fuentes / estilos de notas son predeterminados pero sería fácil de ajustar para otra fuente o estilo)

foreach((array)$data as $sheet_data) { $maxwidth = array( ); $objPHPExcel->setActiveSheetIndex( $i++ ); $sheet = $objPHPExcel->getActiveSheet( ); if ( !empty($sheet_data[''title'']) ) $sheet->setTitle($sheet_data[''title'']); if ( !empty($sheet_data[''rows'']) ) { foreach((array)$sheet_data[''rows''] as $row=>$cols) { foreach((array)$cols as $col=>$val) { $p = strpos($col,'':''); if ( false !== $p ) { // range $range = $col; $xy = substr( $col, 0, $p ); $col = substr($xy,0,-1); // estimate maximum column width by number of characters $w = mb_strlen( $val ); if ( !isset($maxwidth[$col]) ) $maxwidth[$col] = $w; elseif ( $w > $maxwidth[$col] ) $maxwidth[$col] = $w; $sheet->mergeCells( $range ); $sheet->setCellValue( $xy, $val ); $sheet->getStyle( $range ) ->getAlignment( ) ->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_CENTER ) ->setVertical( PHPExcel_Style_Alignment::VERTICAL_CENTER ) ; } else { $xy = $col.$row; // estimate maximum column width by number of characters $w = mb_strlen( $val ); if ( !isset($maxwidth[$col]) ) $maxwidth[$col] = $w; elseif ( $w > $maxwidth[$col] ) $maxwidth[$col] = $w; $sheet->setCellValue( $xy, $val ); $sheet->getStyle( $xy ) ->getAlignment( ) ->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_CENTER ) ->setVertical( PHPExcel_Style_Alignment::VERTICAL_CENTER ) ; } } } } // autosize columns based on calculation + some padding foreach($maxwidth as $col=>$width) { $sheet->getColumnDimension( $col )->setAutoSize( false ); $sheet->getColumnDimension( $col )->setWidth( (int)($width * 1.2) ); // add padding as well } }