Setting width of spreadsheet cell using PHPExcel

后端 未结 7 954
萌比男神i
萌比男神i 2021-01-31 13:20

I\'m trying to set the width of a cell in an Excel document generated with PHPExcel with:

$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn(\'C\')         


        
相关标签:
7条回答
  • 2021-01-31 13:47

    It's a subtle difference, but this works fine for me:

    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
    

    Notice, the difference between getColumnDimensionByColumn and getColumnDimension

    Also, I'm not even setting AutoSize and it works fine.

    0 讨论(0)
  • 2021-01-31 13:47

    setAutoSize method must come before setWidth:

    $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setAutoSize(false);
    $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setWidth('10');
    
    0 讨论(0)
  • 2021-01-31 13:54

    autoSize for column width set as bellow. It works for me.

    $spreadsheet->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
    
    0 讨论(0)
  • 2021-01-31 14:00

    This worked for me:

    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(false);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
    

    be sure to add setAuzoSize(false), before the setWidth(); as Rolland mentioned

    0 讨论(0)
  • 2021-01-31 14:01

    The correct way to set the column width is by using the line as posted by Jahmic, however it is important to note that additionally, you have to apply styling after adding the data, and not before, otherwise on some configurations, the column width is not applied

    0 讨论(0)
  • 2021-01-31 14:07

    Tha is because getColumnDimensionByColumn receives the column index (an integer starting from 0), not a string.

    The same goes for setCellValueByColumnAndRow

    0 讨论(0)
提交回复
热议问题