作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
$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");
关于用于合并单元格的 PHPExcel setAutoSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21333143/
没有合并的代码 $objPHPExcel = new PHPExcel(); $sheet = $objPHPExcel->getActiveSheet(); $sheet->setCellValue
我是一名优秀的程序员,十分优秀!