微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

合并单元格的PHPExcel setAutoSize

代码没有合并

$objPHPExcel = new PHPExcel();
$sheet = $objPHPExcel->getActiveSheet();

$sheet->setCellValueByColumnAndRow(0, 1, 
        "test test test test test test test test");
$sheet->getColumnDimension('A')->setAutoSize(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("test.xlsx");

合并代码

$objPHPExcel = new PHPExcel();
$sheet = $objPHPExcel->getActiveSheet();

$sheet->setCellValueByColumnAndRow(0, 1, 
        "test test test test test test test test");
//this breaks the width calculation
$sheet->mergeCells('A1:B1');
$sheet->getColumnDimension('A')->setAutoSize(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("test.xlsx");

据我所知,没有标准的方法来设置合并单元格的自动大小.这有什么解决方法吗?

解决方法:

您可以在使用 – > calculateColumnWidths()合并单元格之前计算列宽,然后使用 – > setAutoSize(false)来确保不再计算它们.代码可以
看起来像这样:

// Set the data in the cells
$objPHPExcel->getActiveSheet()->fromArray($sheet, null, 'A1');

// Calculate the column widths
foreach(range('A', 'E') as $columnID) {
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
}
$objPHPExcel->getActiveSheet()->calculateColumnWidths();

// Set setAutoSize(false) so that the widths are not recalculated
foreach(range('A', 'E') as $columnID) {
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(false);
}

// Merge cells
$objPHPExcel->getActiveSheet()->mergeCells("A1:A5");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐