PHPExcel 列 A:C 不会自动调整宽度

PHPExcel column A:C wont auto size width

所以我实现了这段代码,生成我的 excel:

$objPHPExcel = new PHPExcel();
$objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //Default = whole workbook center vertical
$tsc = array_keys($data);
for ($i=0; $i < count($tsc); $i++) {
    if ($i != 0) {
        $objPHPExcel->createSheet($i);
    }
    //Active sheet
    $objPHPExcel->setActiveSheetIndex($i);

    $tsc_data = explode(";", $tsc[$i]); //[0] = tsc name - [1] = tsc id
    //Columns merging
    $objPHPExcel->getActiveSheet()->setTitle(substr($tsc_data[0],0,30))
    ->mergeCells('A'.$table_row.':A'.($table_row+3))
    ->mergeCells('B'.$table_row.':B'.($table_row+3))
    ->mergeCells('C'.$table_row.':C'.($table_row+3))
    //merge area
    ->mergeCells('D'.$table_row.':H'.($table_row+2))
    //Adding cell text
    ->SetCellValue('A'.$table_row.'', 'Sr. No.ddddddddddddddddddddddd')
    ->SetCellValue('B'.$table_row.'', 'Component namedddddddddddddddddddddddddddddddddddd')
    ->SetCellValue('C'.$table_row.'', 'Featuresddddddddddddddddddddddd')
    ->SetCellValue('D'.$table_row.'', $tsc_data[0])
    ->SetCellValue('D'.($table_row+3), 'HW Detailsddddddddddddddddddddddd')
    ->SetCellValue('E'.($table_row+3), 'Test Case ID/ Test Stepsddddddddddddddddddddddd')
    ->SetCellValue('F'.($table_row+3), 'Expectedddddddddddddddddddddddd')
    ->SetCellValue('G'.($table_row+3), 'Resultddddddddddddddddddddddd')
    ->SetCellValue('H'.($table_row+3), 'Remarkddddddddddddddddddddddd');
    $objPHPExcel->getActiveSheet()->getStyle('A'.$table_row.':H'.($table_row+3))->applyFromArray($bold_text_12);
    foreach(range('A','H') as $columnID) {
        $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
            ->setAutoSize(true);
    }
}
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('php://output');

如您所见,我确实有自动调整列大小的部分

foreach(range('A','H') as $columnID) {
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
        ->setAutoSize(true);
}

这有效但仅适用于列 D:H 而不是 A:C,我不知道为什么?

您可以在此处看到,当我打开 xml 文件时,列的大小已自动调整,但 A:C

则不会

你们中的任何人都可以看到是什么阻止了这个,因为对我来说它应该有效,但没有?

编辑

我可以看到 A:C 的合并使自动调整大小停止,所以如果我删除它们,这些行会再次自动调整大小...

//These lines
->mergeCells('A'.$table_row.':A'.($table_row+3))
->mergeCells('B'.$table_row.':B'.($table_row+3))
->mergeCells('C'.$table_row.':C'.($table_row+3))
//Do I insert the text into the cells right????
->SetCellValue('A'.$table_row.'', 'Sr. No.ddddddddddddddddddddddd')
->SetCellValue('B'.$table_row.'', 'Component namedddddddddddddddddddddddddddddddddddd')
->SetCellValue('C'.$table_row.'', 'Featuresddddddddddddddddddddddd')

注意:$table_row 默认为 1,因此 'A'.$table_row.':A'.($table_row+3)A1:A4

找到了一个解决方案,所以我现在将合并部分放在我的代码底部,并且在我这样做之前,我自动调整所有列的大小,然后再次关闭它,所以合并不会重新计算宽度:

foreach(range('A','H') as $columnID) {
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
}
$objPHPExcel->getActiveSheet()->calculateColumnWidths();

// Set setAutoSize(false) so that the widths are not recalculated
foreach(range('A','H') as $columnID) {
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(false);
}
//Columns merging (HAS TO BE AT THE END ELSE AUTO SEIZE WONT WORK)
$objPHPExcel->getActiveSheet()->setTitle(substr($tsc_data[0],0,30))
->mergeCells('A'.$table_row.':A'.($table_row+3))
->mergeCells('B'.$table_row.':B'.($table_row+3))
->mergeCells('C'.$table_row.':C'.($table_row+3))
//merge area
->mergeCells('D'.$table_row.':H'.($table_row+2));