php PHPExcel - 合并单元格的动态行高

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13313048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:08:33  来源:igfitidea点击:

PHPExcel - dynamic row height for merged cells

phpphpexcel

提问by Law Hop

After a lot of trial and error, I still can't seem to figure out a workaround to get merged cells to have an AutoFit height.

经过大量的反复试验,我似乎仍然无法找到一种解决方法来使合并的单元格具有 AutoFit 高度。

I've tried an approach based on a bit of VBA code I found at this site: https://groups.google.com/forum/?fromgroups=#!topic/microsoft.public.excel.programming/pcvg7o5sKhA

我尝试了一种基于我在此站点上找到的一些 VBA 代码的方法:https: //groups.google.com/forum/?fromgroups=#!topic/microsoft.public.excel.programming/pcvg7o5sKhA

The following code pastes the text, wraps it, and changes the width of the cell (A1) to the total width of the merged cells I want. Then, it merges the cells and sets column A back down to the original width. $note is any long string of text. $vAlignTop is an array setting the alignment the text to the top of the cell.

以下代码粘贴文本,将其换行,并将单元格 (A1) 的宽度更改为我想要的合并单元格的总宽度。然后,它合并单元格并将列 A 设置回原始宽度。$note 是任何长文本字符串。$vAlignTop 是一个数组,设置文本与单元格顶部的对齐方式。

$totalWidth = 67.44; //width of columns A-H

$objPHPExcel->getActiveSheet()->setCellValue('A1', $note);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth($totalWidth);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->applyFromArray($vAlignTop);
$objPHPExcel->getActiveSheet()->mergeCells('A1:H1');
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8.43); //original width of column A

When I go through these same steps manually in excel, I get the result I want, but the output of the code above is always the default 12.75 row height.

当我在 excel 中手动执行这些相同的步骤时,我得到了我想要的结果,但上面代码的输出始终是默认的 12.75 行高。

Anyone have any ideas? I don't really mind having to hardcode the column widths, i just want the height to be responsive to the text.

谁有想法?我真的不介意必须对列宽进行硬编码,我只希望高度能够响应文本。

Thanks in advance.

提前致谢。

回答by ken clark

Autoheight doesn't work on merged cells. I think this is a problem with Excel not PHPExcel. If you want to do this you must use a work around. This is mine...

Autoheight 不适用于合并的单元格。我认为这是 Excel 的问题,而不是 PHPExcel。如果你想这样做,你必须使用变通方法。这是我的...

I have a function that takes text, splits into lines on newlines ('\n') and calculates the number of rows needed to 'fit' the text based on the number of characters per line (fiddle factor).

我有一个函数,它接受文本,在换行符('\n')上分成几行,并根据每行的字符数(小提琴因子)计算“适合”文本所需的行数。

function getRowcount($text, $width=55) {
    $rc = 0;
    $line = explode("\n", $text);
    foreach($line as $source) {
        $rc += intval((strlen($source) / $width) +1);
    }
    return $rc;
}

For my report the fiddle-factor, arrived at by trial and error, is 55. I then use the above function in my code...

对于我的报告,通过反复试验得出的小提琴因子是 55。然后我在我的代码中使用上述函数......

$purpose = $survey["purpose"];
$numrows = getRowcount($purpose);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$xlrow, 'Report Purpose');
$objPHPExcel->getActiveSheet()->getStyle('B'.$xlrow)->applyFromArray($fmt_cover_bold);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$xlrow, $purpose);
$objPHPExcel->getActiveSheet()->getRowDimension($xlrow)->setRowHeight($numrows * 12.75 + 2.25);
$objPHPExcel->getActiveSheet()->mergeCells('C'.$xlrow.':E'.$xlrow);
$objPHPExcel->getActiveSheet()->getStyle('C'.$xlrow.':E'.$xlrow)->applyFromArray($fmt_normal_wrap);
$xlrow++;

I add 2.25 just to give a little separation between this cell and the next.

我添加 2.25 只是为了在这个单元格和下一个单元格之间做一点分隔。

回答by Martin Suchan

I think, that I found better solution. When I inserting data to merged columns in foreach, I pick the longest string in row by comparing with strlen(). After that insert the longest string to the last+1 column and set it to hidden column. There is example for row 1 with 4 columns (A-D)

我认为,我找到了更好的解决方案。当我在 foreach 中将数据插入合并列时,我通过与 strlen() 进行比较来选择行中最长的字符串。之后将最长的字符串插入最后+1 列并将其设置为隐藏列。第 1 行有 4 列 (AD) 的示例

$longestContent = "the longest content in row 1";
$mergerColumnsWidth = 24;
$objPHPExcel->getActiveSheet()->setCellValue("E1", $longestContent);
$objPHPExcel->getActiveSheet()->getStyle("E1")->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getColumnDimension("E")->setWidth(mergerColumnsWidth );
$objPHPExcel->getActiveSheet()->getColumnDimension("E")->setVisible(false);

回答by bn00d

To set the Auto Height for any row use:

要为任何行设置自动高度,请使用:

$_row_number = 10;

$excel->getActiveSheet()->getRowDimension($_row_number)->setRowHeight(-1);

i.e. set the parameter to -1 instead of any number.

即将参数设置为 -1 而不是任何数字。