如何PHPExcel设置自动列宽


How to PHPExcel set auto-columns width

我正在使用PHPExcel导出数据以供下载。当打开下载的文件时,单元格的数字很大,它会显示"#####"而不是值号。我尝试setAutoSize()每一列然后调用$sheet->calculateColumnWidths()但它仍然没有改变。我在这里看到calculateColumnWidths(),@Mark Baker说"calculateColumnWidths()将值增加5%,以尝试确保整个列适合"。如果单元格中的数字长度超过 5%,似乎解决了问题

更新这是我自动调整列大小的函数:

   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();
    }

第一个潜在的问题可能是您正在使用列字母。PHP 的增量器操作将处理列字母,因此如果$i是"A",则 $i++ 将给出"B",如果$i是"Z",则 $i++ 将给出"AA";但您不能使用 <= 作为比较器,因为"AA"在作为直接比较执行时是 <= "Z"。

而不是

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

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

要在调用 $sheet->calculateColumnWidths() 后添加 5% 的边距,请执行以下操作:

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

没有一个建议对我有用,所以我做了一个手动计算(相当简单和快速)(示例代码如下)并且工作得很好(注意字体/样式是默认的,但很容易调整其他字体或样式)

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
    }
}