Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set font in decimal number using HSSFFont

I want to set text font to 7.5 to a particular text in excel sheet. I am using below code to prepare the Font.

HSSFCellStyle txtStyle = (HSSFCellStyle)workbook.createCellStyle();
HSSFFont txtFont = (HSSFFont)workbook.createFont();
txtFont.setFontName("Arial");
txtFont.setFontHeightInPoints((short)7.5);
txtStyle.setFont(txtFont);

But it always takes font as 7 because of short type casting and hence targeted text in excel sheet is taking font 7 and not 7.5. I also tried method 'setFontHeight' but that also takes short as parameter. So is there any way by which I could set text font to decimal number?

like image 468
Nags Avatar asked Feb 16 '15 10:02

Nags


1 Answers

Promoting comments to an answer - the POI HSSF Font class has two font size settings methods:

  • setFontHeight(short) - Set the font height in unit's of 1/20th of a point
  • setFontHeightInPoints(short) - Set the font height in point

Using setFontHeightInPoints is the easier one for most cases, and is recommended in the Javadocs. However, it'll only cope with whole-number font heights. That's most font sizes, but not all

To set a font height of 7.5, you'd need to change your code instead to be:

xtFont.setFontHeight((short)(7.5*20));

That uses the alternate one that takes 1/20 point sizes, so copes with non-integer values.

like image 62
Gagravarr Avatar answered Oct 19 '22 23:10

Gagravarr