0

I need to generate a MS Excel 2007+ file from my application according to a given pattern provided by management. Most of the work is done, but I have to set the column width to 11,34 centimeters. I tried to use the setColumnWidth(int columnIndex, int width) method, but regardless of the value I provide it does not work. JavaDoc says:

Set the width (in units of 1/256th of a character width)

The maximum column width for an individual cell is 255 characters. This value represents the number of characters that can be displayed in a cell that is formatted with the standard font.

How to do this?

2
  • 1
    Did you check to make sure that the column isn't being set to autosize automatically? Make sure that you are not doing this call in your code, sheet.autoSizeColumn(columnIndex); Jun 1, 2011 at 13:07
  • Hi, I haven't found any similar calls on my code. I'm wondering if there's a way to convert centimeters to "character width" instead? Or this doesn't makes sense, whatsoever?
    – gtludwig
    Jun 1, 2011 at 16:55

2 Answers 2

1

Ok, I think I understand your problem now, your business requirements state that you must set the width of the column based on standard Excel font character width.

The way I would approach this would be to use the Print Screen key on an example in Excel, then paste into Paint or a similar program and count pixel width. I would then use the ruler tool to measure pixels/cm and use the resulting number as my fixed column width.

This will probably involve a little bit of trial and error to get right.

Now with that said I will share my personal opinion that I feel this a LAZY and WEAK business requirement. What is "standard font"? Which version of Excel? This requirement is open to entirely too much developer interpretation.

1
  • The method I use to build the sheet's layout is [link]pastebin.com/iaqWhHg6[/link]. But, the requirement I have is to make the output so my manager can just copy-paste the resulting formatted set of columns into a report to the board. The current standard is so one of the colums is much bigger (the code does not reflect this), but I need to programmatically set the column's width.
    – gtludwig
    Jun 1, 2011 at 17:29
0

Actually, I was mistaken from the very beggining! My (terrible!) bad! Width columns were already being set, but thought not! All it took as a simple "rule of three" and I was set.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.