public final class ExcelFont extends Object
Conceptually, cell formatting is divided into following groups:
CellStyle.setNumberFormat(java.lang.String)
.
CellStyle.setHorizontalAlignment(com.gembox.spreadsheet.HorizontalAlignmentStyle)
, CellStyle.setVerticalAlignment(com.gembox.spreadsheet.VerticalAlignmentStyle)
, CellStyle.setIndent(int)
, CellStyle.setRotation(int)
, CellStyle.setTextVertical(boolean)
, CellStyle.setWrapText(boolean)
and CellStyle.setShrinkToFit(boolean)
.
CellStyle.setFont(com.gembox.spreadsheet.ExcelFont)
.
CellStyle.setBorders(com.gembox.spreadsheet.CellBorders)
.
CellStyle.setFillPattern(com.gembox.spreadsheet.ExcelFillPattern)
.
CellStyle.setLocked(boolean)
and CellStyle.setFormulaHidden(boolean)
.
Additional CellStyle
methods not associated with any formatting group are:
CellStyle.getName()
- name of the referenced workbook style
.
CellStyle.setQuotePrefix(boolean)
- true
to store numeric value of a cell as text; otherwise, false
.
CellStyle.isDefault()
- true
if referenced
workbook style
is default (
CellStyleCollection.getNormal()
) and there are no additional modifications of cell formatting; otherwise, false
.
Workbook
contains a set of master styles
which can be referenced by multiple cells.
Workbook
must always contain at least one master style
which cannot be removed and is, by default, referenced by all cells. This default style is
CellStyleCollection.getNormal()
.
Workbook style
can either be built-in or user-defined. Built-in style is accessible from workbook styles
via BuiltInCellStyleName
enumeration.
Cell formatting group (Number, Alignment, Font, Border, Fill or Protection) (and its associated methods) is resolved from referenced
workbook (master) style
, unless cell formatting group or its associated method is modified.
Cell formatting is available for one or more cells through AbstractRange.setStyle(com.gembox.spreadsheet.CellStyle)
method which is available on ExcelCell
and CellRange
types.
Cell formatting specified on ExcelColumn
and ExcelRow
types through ExcelColumnRowBase.setStyle(com.gembox.spreadsheet.CellStyle)
method is simply propagated to cell formatting of its ExcelColumnRowBase.getCells()
.
For performance reasons, cell formatting on CellRange
is resolved based just on its top-left cell formatting, except borders which are resolved based on corner cells depending on border side.
Setting cell formatting method on CellRange
is propagated to each cell in a range.
To set workbook (master) style
to one or more cells, simply assign it to AbstractRange.setStyle(com.gembox.spreadsheet.CellStyle)
method.
Preferable way to modify formatting method of multiple cells is to get CellRange
to which all those cells belong, and use AbstractRange.setStyle(com.gembox.spreadsheet.CellStyle)
method of that range to make the modification.
If modifying multiple formatting properties of a CellRange
, without preserving unmodified formatting properties, preferable way is to create new instance of CellStyle
, make modifications on it, and assign it using AbstractRange.setStyle(com.gembox.spreadsheet.CellStyle)
method of that range.
GemBox.Spreadsheet internally takes care not to allocate unnecessary cells when formatting a range of cells (for example, when formatting all worksheet cells
) and to cache formatting information of equally formatted cells, at the appropriate time, to reduce memory footprint.
// 'Font' formatting group
sheet.getCell("B13").setValue("Font.Name = ");
sheet.getCell("C13").setValue("Comic Sans MS");
sheet.getCell("C13").getStyle().getFont().setName("Comic Sans MS");
sheet.getCell("B14").setValue("Font.Italic = ");
sheet.getCell("C14").setValue("true");
sheet.getCell("C14").getStyle().getFont().setItalic(true);
sheet.getCell("B15").setValue("Font.Weight = ");
sheet.getCell("C15").setValue("ExcelFont.BoldWeight");
sheet.getCell("C15").getStyle().getFont().setWeight(ExcelFont.BOLD_WEIGHT);
sheet.getCell("B16").setValue("Font.Size = ");
sheet.getCell("C16").setValue("18 * 20");
sheet.getCell("C16").getStyle().getFont().setSize(18 * 20);
sheet.getCell("B17").setValue("Font.Color");
sheet.getCell("C17").setValue("Text2");
sheet.getCell("C17").getStyle().getFont().setColor(SpreadsheetColor.fromName(ColorName.TEXT_2));
sheet.getCell("B18").setValue("Font.UnderlineStyle = ");
sheet.getCell("C18").setValue("UnderlineStyle.Double");
sheet.getCell("C18").getStyle().getFont().setUnderlineStyle(UnderlineStyle.DOUBLE);
sheet.getCell("B19").setValue("Font.Strikeout = ");
sheet.getCell("C19").setValue("true");
sheet.getCell("C19").getStyle().getFont().setStrikeout(true);
sheet.getCell("B20").setValue("Font.ScriptPosition = ");
sheet.getCell("C20").setValue("ScriptPosition.Superscript");
sheet.getCell("C20").getStyle().getFont().setScriptPosition(ScriptPosition.SUBSCRIPT);
CellStyle.getFont()
Modifier and Type | Field and Description |
---|---|
static int |
BOLD_WEIGHT
Default bold font weight.
|
static int |
MAX_WEIGHT
Maximum font weight.
|
static int |
MIN_WEIGHT
Minimum font weight.
|
static int |
NORMAL_WEIGHT
Normal font weight.
|
Constructor and Description |
---|
ExcelFont()
Initializes a new instance of the
ExcelFont class. |
Modifier and Type | Method and Description |
---|---|
SpreadsheetColor |
getColor()
Gets the color of the text.
|
String |
getName()
Gets the face name of this font.
|
ScriptPosition |
getScriptPosition()
Gets the vertical position of the text relative to the text's default appearance.
|
int |
getSize()
Gets the size of the text in twips (1/20th of a point).
|
UnderlineStyle |
getUnderlineStyle()
Gets the underline formatting of the text.
|
int |
getWeight()
Gets the font weight (font boldness).
|
boolean |
isItalic()
Gets a value indicating whether to display characters in italic font style.
|
boolean |
isStrikeout()
Gets a value indicating whether to draw a strike-through line through the horizontal middle of the text.
|
void |
setColor(SpreadsheetColor value)
Sets the color of the text.
|
void |
setItalic(boolean value)
Sets a value indicating whether to display characters in italic font style.
|
void |
setName(String value)
Sets the face name of this font.
|
void |
setScriptPosition(ScriptPosition value)
Sets the vertical position of the text relative to the text's default appearance.
|
void |
setSize(int value)
Sets the size of the text in twips (1/20th of a point).
|
void |
setStrikeout(boolean value)
Sets a value indicating whether to draw a strike-through line through the horizontal middle of the text.
|
void |
setUnderlineStyle(UnderlineStyle value)
Sets the underline formatting of the text.
|
void |
setWeight(int value)
Sets the font weight (font boldness).
|
String |
toString()
Returns a
String that represents this ExcelFont instance. |
public static final int BOLD_WEIGHT
getWeight()
,
Constant Field Valuespublic static final int MAX_WEIGHT
getWeight()
,
Constant Field Valuespublic static final int MIN_WEIGHT
getWeight()
,
Constant Field Valuespublic static final int NORMAL_WEIGHT
getWeight()
,
Constant Field Valuespublic ExcelFont()
ExcelFont
class.public SpreadsheetColor getColor()
Gets the color of the text.
Default value is color created from name ColorName.TEXT_1
.
public String getName()
Gets the face name of this font.
Default value is Calibri.
public ScriptPosition getScriptPosition()
Gets the vertical position of the text relative to the text's default appearance.
Default value is ScriptPosition.NORMAL
.
public int getSize()
Gets the size of the text in twips (1/20th of a point).
Default value is 11 * 20 = 220.
public UnderlineStyle getUnderlineStyle()
Gets the underline formatting of the text.
Default value is UnderlineStyle.NONE
.
public int getWeight()
Gets the font weight (font boldness).
Default value is NORMAL_WEIGHT
.
public boolean isItalic()
Gets a value indicating whether to display characters in italic font style.
Default value is false
.
true
to display characters in italic font style; otherwise, false
.public boolean isStrikeout()
Gets a value indicating whether to draw a strike-through line through the horizontal middle of the text.
Default value is false
.
true
to draw a strike-through line through the horizontal middle of the text; otherwise, false
.public void setColor(SpreadsheetColor value)
Sets the color of the text.
Default value is color created from name ColorName.TEXT_1
.
value
- The color of the text.public void setItalic(boolean value)
Sets a value indicating whether to display characters in italic font style.
Default value is false
.
value
- true
to display characters in italic font style; otherwise, false
.public void setName(String value)
Sets the face name of this font.
Default value is Calibri.
value
- The face name of this font.IllegalArgumentException
- Value cannot be null or empty.public void setScriptPosition(ScriptPosition value)
Sets the vertical position of the text relative to the text's default appearance.
Default value is ScriptPosition.NORMAL
.
value
- The vertical position of the text relative to the text's default appearance.public void setSize(int value)
Sets the size of the text in twips (1/20th of a point).
Default value is 11 * 20 = 220.
value
- The size of the text.IndexOutOfBoundsException
- Value must be positive.public void setStrikeout(boolean value)
Sets a value indicating whether to draw a strike-through line through the horizontal middle of the text.
Default value is false
.
value
- true
to draw a strike-through line through the horizontal middle of the text; otherwise, false
.public void setUnderlineStyle(UnderlineStyle value)
Sets the underline formatting of the text.
Default value is UnderlineStyle.NONE
.
value
- The underline formatting of the text.public void setWeight(int value)
Sets the font weight (font boldness).
Value must be between MIN_WEIGHT
and MAX_WEIGHT
.
For standard boldness, use BOLD_WEIGHT
.
Default value is NORMAL_WEIGHT
.
value
- The font weight (font boldness).IndexOutOfBoundsException
- Value must be between MAX_WEIGHT
and MAX_WEIGHT
.© GemBox Ltd. — All rights reserved.