Apache POI宽度计算 [英] Apache POI Width calculations

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

问题描述

由于我不了解文档,我在获取columnWidth和计算的行高时需要精确的尺寸时遇到问题.

I'm having problems getting the exact size I want with my calculations for columnWidth and my calculated row height because I don't understand the documentation.

就columnWidth而言,我正在使用代码行sheet.setColumnWidth(int columnIndex, int width);,但我不了解如何正确计算宽度.我明白了,它说:

In terms of columnWidth, I am using the line of code sheet.setColumnWidth(int columnIndex, int width); but I don't understand how to properly calculate width. I get that it says:

width = Truncate([{Number of Visible Characters} * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256

因此,我有一个要创建的精确Excel工作表示例,当我突出显示一列并选择列宽时,它告诉我它是9.67 (94 pixels).那么这是什么意思?如何将其插入方程式以获得所需的宽度值?

So I have an exact Excel sheet example of what I want to create and when I highlight a column and select column width it tells me it is 9.67 (94 pixels). So what does this mean? How do I plug this into my equation to get the value of width I want?

我遇到的另一个问题是,我正在使用在其他地方(甚至在SO上)找到的代码来动态计算行高.但是我的问题是我不明白下面代码中的mergedCellWidth应该在表示nextPos = measurer.nextOffset(mergedCellWidth)的行中是什么.我似乎无法完全正确地获得此值,并且弄乱了它认为应该有多少行,因此我的行高不正确.

The other problem I am having is that I'm using code I found elsewhere (even on SO) to dynamically calculate row height. But my problem is I don't understand what mergedCellWidth in the below code should be in the line that says nextPos = measurer.nextOffset(mergedCellWidth). I can't seem to get this value quite right and it's messing up how many lines it thinks there should be and therefore my row height isn't right.

java.awt.Font currFont = new java.awt.Font("Calibri", 0, 11);
AttributedString attrStr = new AttributedString(record.getDescription());
attrStr.addAttribute(TextAttribute.FONT, currFont);

// Use LineBreakMeasurer to count number of lines needed for the text
FontRenderContext frc = new FontRenderContext(null, true, true);
LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc);
int nextPos = 0;
int lineCnt = 0;

while (measurer.getPosition() < record.getDescription().length()) {
    System.out.println(measurer.getPosition());
    nextPos = measurer.nextOffset(mergedCellWidth); // mergedCellWidth is the max width of each line
    lineCnt++;
    measurer.setPosition(nextPos);
    System.out.println(measurer.getPosition());
}

row.setHeight((short)(row.getHeight() * lineCnt));

我认为,就我而言,示例将是我的最佳答案.谢谢!

I think in my case examples would be the best answer for me. Thanks!

推荐答案

似乎没有人真的可以帮助我.经过反复试验,我发现一些近似的方法对我来说很有效.

Well it doesn't seem like anyone really can help me out. After trial and error, I've found a couple of approximations that have worked ok for me.

关于我的sheet.setColumnWidth(int columnIndex, int width);问题,我发现一个很好的近似值如下:

Concerning my sheet.setColumnWidth(int columnIndex, int width); problem, I found a good approximation is as follows:

  1. 在Excel中,转到普通"视图.
  2. 转到格式->列宽..."并记下值
  3. 使用以下公式计算宽度:width = ([number from step 2] * 256) + 200

这实际上似乎工作得很好.

This actually seems to work fairly well.

关于我的第二个问题是mergedCellWidth应该是什么,我发现该值可以正常工作:(float) (sheet.getColumnWidth(columnIndex) / 256 * 4).对于长度较小的字符串,它的效果要好得多,并且随着字符串的变大,行开始变得太大(因此我认为4的因数有点太大,但可以满足我的目的).

Concerning my second problem about what mergedCellWidth should be, I found the following works ok for that value: (float) (sheet.getColumnWidth(columnIndex) / 256 * 4). It works much better for smaller length strings and as the strings get larger, the row starts to get a little too big (so I assume the factor of 4 is a little too much but it works for my purposes).

我也确实检查了Apache POI源中的自动调整列大小,以试图了解正在发生的事情.尽管单位的含义并没有很多指导,但是我无法遵循它们的计算结果并无法在Excel模板中进行复制.

I also did check out the Apache POI source for auto sizing columns to try to get an idea of what was going on. There isn't a lot of direction though into what the units mean and I wasn't able to follow their calculations and reproduce them in my Excel template.

这篇关于Apache POI宽度计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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