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