PHPExcel 中的自动调整列然后添加大标题文本而不会弄乱宽度

Autofit Columns in PHPExcel then add large title text without messing up widths

我正在使用 PHPExcel 创建电子表格。我想添加数据,然后将列宽设置为 setAutoSize(true) ... 然后 我想向页面添加一个不可避免地大于其列的标题。我遇到的问题是该列自动调整为标题的宽度,即使我在 setAutoSize(true) 调用之后添加它也是如此。

我已经尝试将该列的自动调整大小设置回 false,这只会将该列恢复为默认宽度。我试过将自动大小设置为 true,获取列的宽度,然后将自动大小设置为 false,然后设置列的宽度。这只是将宽度设置为上述默认值。下面是我的代码片段...

    while($row = $this->getRow()){

        ++$currentrow;
        for($i = 0;$i < count($row); $i++){
                $sheet->setCellValueByColumnAndRow($i + 1, $currentrow,$row[$i]);
        }
    }

    // now that we have put all the data in the spreadsheet, auto fit the columns...

    for($i = 1;$i <= $this->columnCount; $i++){

        // this bit converts an integer into an excel column (such as 2 = 'B' or 28 = 'AB')
        if($i + 64 > 90){
            $col = "A" . chr($i + 38);
        }else{
            $col = chr($i + 64);
        }

        $sheet->getColumnDimension($col)->setAutoSize(true);  
        /* this is causing the column containing the title to autosize
        to the title's width after the title is added further down the code.*/

    }

    $titlecolwidth = $sheet->getColumnDimension('B')->getWidth();
    $sheet->getColumnDimension('B')->setAutoSize(false);
    $sheet->getColumnDimension('B')->setWidth($titlecolwidth);

    // Add the heading...

    if(isset($heading)){    
        $sheet->setCellValueByColumnAndRow(1, 2,$heading);  
        $sheet->getCellByColumnAndRow(1,2)->getStyle()->getFont()->setSize(20);
    }

在 excel 中手动创建电子表格时,这类事情很容易实现 - 添加数据,执行自动调整列,然后添加标题。包含标题的列保持其先前设置的宽度(添加标题之前)

有没有一种方法可以使用 phpexcel 来达到同样的效果?

我确实浏览了文档,但找不到自动调整该列的内容,但在执行调整后又不理会它。

我通过查看另一个 Whosebug 问题 (How to PHPExcel set auto-columns width) 找到了如何自己做。

如果我在自动调整大小后调用 $sheet->calculateColumnWidths();,那么 'getwidth' 调用将 return 一个有效宽度,我的代码将起作用....

    // added line...
    $sheet->calculateColumnWidths();

    //original code...
    $titlecolwidth = $sheet->getColumnDimension('B')->getWidth();
    $sheet->getColumnDimension('B')->setAutoSize(false);
    $sheet->getColumnDimension('B')->setWidth($titlecolwidth);

None 的建议对我有用,所以我进行了手动计算(相当简单和快速)(示例代码如下)并且效果很好(注意 fonts/styles 是默认值,但它会很容易调整其他字体或样式)

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