php_excel07-如何根据单元格数据增加单元格的高度(xls)

标签 php phpexcel

在我的应用程序中,我需要以预定义格式导出到 xls 文件。

所以我刚刚集成了 php_excel2007。我正在使用一个具有预定义格式的模板。

问题 这里的单元格数据可能会动态变化。如果数据远大于单元格高度,则数据正在崩溃。

那么是否可以根据单元格的内容(在 XLX 中而不是 xlsx 中)增加单元格的高度?

最佳答案

唯一的方法如 this answer 中所述对于您之前关于此主题的问题:将行高设置为自动调整,并将单元格对齐方式设置为换行。这应该以相同的方式工作,无论您使用 Excel5 Writer 还是 Excel2007 Writer。

$objPHPExcel = new PHPExcel();

// Set some long string values in some cells
$objPHPExcel->getActiveSheet()->getCell('A1')->setValue("This is a large block of text,\ncomprising several lines,\nthat will be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('A2')->setValue("This is a large block of text that will NOT be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('B1')->setValue("This is another large block of text without any line breaks, that will be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('A3')->setValue("This is another large block of text,\ncomprising several lines,\nthat will be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('A4')->setValue("This is another large block of text without any line breaks, that will be set to autofit but not wrap.");
// Fix the column width to a reasonable size
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
//  Set text wrap for cells A1 and B1. This forces the text to wrap, but doesn't adjust the height of the row
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setWrapText(true);
// Set rows 1, 3 and 4 to autofit the height to the size of text
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getRowDimension(3)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getRowDimension(4)->setRowHeight(-1);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('testAutoHeight.xls');

编辑

在代码示例中添加了注释以解释每个调用的方法实际做了什么

关于php_excel07-如何根据单元格数据增加单元格的高度(xls),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3913926/

相关文章:

phpexcel - 为什么在尝试通过 PHP 读取 excel 文件时会收到 "invalid or unitialized Zip object"错误?

php - 按月排序

php - 如何在 Laravel 中检索当前日期的数据

phpexcel - 自定义 PHPExcel 单元格格式仅在双击后显示

phpEXCEL getCalculatedValue 未返回所需结果

php - PHPExcel 的问题

php - 我如何解决存储过程中不支持 "LOAD DATA"和/或 MySQL 中的 TRANSACTION 中缺少 EXIT HANDLER 的问题?

php - Laravel 5 - env local debug true 没有显示错误

通过 ftp(Netbeans、Eclipse 或任何免费的 IDE)的 php 项目

php - PHP 在 Foreach 循环中是否有 "built-in"迭代器?