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?
Promoting comments to an answer - the POI HSSF Font class has two font size settings methods:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With