PHPExcel-合并单元格的动态行高 [英] PHPExcel - dynamic row height for merged cells

查看:253
本文介绍了PHPExcel-合并单元格的动态行高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量的试验和错误,我似乎仍然找不到解决办法,以使合并的单元格具有AutoFit高度.

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.

我尝试了一种基于在此站点上找到的VBA代码的方法:

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

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

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.

$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

当我在excel中手动执行这些步骤时,会得到所需的结果,但是上面代码的输出始终是默认的12.75行高.

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.

有人有什么想法吗?我真的不介意对列的宽度进行硬编码,我只是希望高度能够响应文本.

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.

谢谢.

推荐答案

自动高度不适用于合并的单元格.我认为这是Excel而不是PHPExcel的问题.如果要执行此操作,则必须使用变通方法.这是我的...

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...

我有一个函数,该函数接收文本,在换行符('\ n')上分成几行,并根据每行的字符数(小数因子)计算适合"文本所需的行数.

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).

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

在我的报告中,经过反复试验得出的错误因素是55.然后在代码中使用上面的函数...

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

$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++;

我添加2.25只是为了在此单元格和下一个单元格之间提供一些分隔.

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

这篇关于PHPExcel-合并单元格的动态行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆