java 如何使用 HSSFFont 以十进制数设置字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28539726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:47:14  来源:igfitidea点击:

How to set font in decimal number using HSSFFont

javaapache-poi

提问by Nags

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.

我想将文本字体设置为 7.5 到 Excel 工作表中的特定文本。我正在使用下面的代码来准备字体。

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?

但由于短类型转换,它总是采用字体 7,因此 Excel 表中的目标文本采用字体 7 而不是 7.5。我也尝试过方法“setFontHeight”,但它也将 short 作为参数。那么有什么方法可以将文本字体设置为十进制数?

回答by Gagravarr

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

提升对答案的评论 - POI HSSF 字体类有两种字体大小设置方法:

Using setFontHeightInPointsis 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

在大多数情况下,使用setFontHeightInPoints是一种更容易的方法,并且在 Javadocs 中被推荐使用。但是,它只能处理整数字体高度。这是大多数字体大小,但不是全部

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

要将字体高度设置为 7.5,您需要将代码更改为:

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

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

它使用采用1/20点大小的替代方法,因此可以处理非整数值。