phpExcel列大小问题 [英] phpExcel column size issues

查看:127
本文介绍了phpExcel列大小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保phpExcel生成的工作簿中没有任何值被截断.为此,我打电话

I want to make sure that no value is cut off in a workbook produced by phpExcel. To do this I call

  $oCurrentSheet->getColumnDimension($aCurrentCoords['x'])->setAutoSize(true);

每次我写一个单元格. 这似乎不起作用:许多列最终都变得很小.

every time I write a cell. This doesn't seem to work: Many columns end up really tiny.

有人知道如何确保输出的excel工作簿中的每一列都根据其内容调整大小吗?

Does anybody know how to make sure every column in the outputted excel workbook is sized according to its content?

推荐答案

不要在每次编写单元格时都执行此操作……这是不必要的开销.每列只执行一次.

Don't do this every time you write a cell... it's an unnecessary overhead. Do it just once for each column.

请注意,保存时仍然很昂贵,因为PHPExcel随后会遍历该列中的每个单元格,并根据该单元格的内容计算出必要的宽度,并考虑公式计算和格式设置,并使用任何该列中的单元格.即使那样,它也不是一个完美的解决方案:默认行为是估算,计算要显示的字符数,并使用基于字体的通用乘法器(Arial,Calibri的Verdana)和一点点填充.为了更精确地计算,您可以更改用于计算的方法,然后代码使用GD来构建ttf边界框,并将像元大小基于该框的尺寸.这是一个更准确的计算,但速度慢得多.

Be warned, it's still expensive when saving, because PHPExcel then walks through each cell in that column working out the width necessary based on the content of that cell, taking into account formula calculation and formatting, and using the maximum size of any cell in that column. Even then it isn't a perfect solution: the default behaviour is to estimate, counting the number of characters to display, and using a generic multiplier based on font (Arial, Verdana of Calibri) and a little bit of padding. For a more accurate calculation, you can change the method used for the calculation, and the code then uses GD to build a ttf bounding box and base the cell size on the dimensions of that box. This is a more accurate calculation, but a lot slower.

您可以使用以下方法更改计算方法:

You can change the calculation method using:

PHPExcel_Shared_Font::setAutoSizeMethod($method);

保存之前,其中$ method是以下之一:

before saving, where $method is one of the following:

PHPExcel_Shared_Font::AUTOSIZE_METHOD_APPROX
PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT

这仍然不是一个完美的解决方案,但这是PHPExcel可以做的最好的计算.

This still isn't a perfect solution, but it's the best calculation that PHPExcel can do.

另一种方法是在保存之前直接调用工作表的calculateColumnWidths()方法,然后遍历每列的维,将值增加大约5%,以尝试在保存前确保整个列都适合.

An alternative is to call the worksheet's calculateColumnWidths() method yourself directly before saving... and then walk through each column's dimension increasing the value by perhaps 5% to try and ensure that the entire column fits, before saving.

这篇关于phpExcel列大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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