Class CellStyle.Builder
- Enclosing class:
- CellStyle
CellStyle. Only the attributes you set are
sent to the engine. Every other attribute takes a fixed default: font
Calibri 11 pt, not bold, italic or underlined, automatic font color,
number format General, no fill, no borders, general horizontal and bottom
vertical alignment, no wrap, no rotation, no indent, locked and not hidden.
Unset font attributes do not follow Workbook.setDefaultFont.
Range.setStyle and Cell.setStyle replace each cell's
whole style with the built one; they do not merge it into the old style.
So a style from CellStyle.builder() that sets only bold also resets
the cell's fill, borders and number format. To change one attribute and
keep the rest, start from the current style with
CellStyle.toBuilder().
A null string attribute, a negative rotation or indent, or a
character set or family outside 0-255 makes setStyle throw
AxyraException.
Range header = sheet.range("A1:D1");
CellStyle style = header.style().toBuilder() // keep font, borders, format
.bold()
.fill(Color.rgb(0xDD, 0xEB, 0xF7))
.horizontalAlign(HAlign.CENTER)
.build();
header.setStyle(style);
header.style() reads the top-left cell, so every cell of the
range gets that cell's style plus the changes.
-
Method Summary
Modifier and TypeMethodDescriptionallBorders(BorderStyle style, Color color) Sets the left, right, top and bottom edges of each cell to the same line.bold()Makes the font bold; shorthand forbold(true).bold(boolean b) Sets (true) or clears (false) bold.border(Side side, BorderStyle style, Color color) Sets one border edge.build()Builds an immutable style; later builder calls do not change it.diagonalDown(boolean on) Shows (true) or hides the diagonal line from top-left to bottom-right.diagonalUp(boolean on) Shows (true) or hides the diagonal line from bottom-left to top-right.fillPattern(PatternType pattern, Color foreground, Color background) Sets a patterned fill:patterndrawn inforegroundoverbackground.fontCharset(int charset) Sets the font character set code (0 = ANSI, 1 = DEFAULT, 128 = Shift-JIS, …).Sets the font color.fontFamily(int family) Sets the font family class: 0 = not applicable, 1 = Roman, 2 = Swiss, 3 = Modern, 4 = Script, 5 = Decorative.Sets the font family name, e.g.fontScheme(String scheme) Sets the theme font scheme the font belongs to.fontSize(double points) Sets the font size.Sets the font position: baseline, superscript or subscript.gradientFill(int degree, Color... colors) Sets a linear gradient fill acrosscolors, spread evenly from position 0 to 1.hidden(boolean b) Hides the formula from the formula bar while the sheet is protected (true).Sets the horizontal alignment.indent(int level) Sets the indent level.italic()Makes the font italic; shorthand foritalic(true).italic(boolean b) Sets (true) or clears (false) italic.locked(boolean b) Locks (true, the default) or unlocks the cell for sheet protection.numberFormat(String code) Sets the Excel number format code, e.g.pathGradientFill(Color... colors) Sets a path gradient fill that converges at the cell center (Excel's "From center" gradient), acrosscolorsspread evenly from position 0 to 1.shrinkToFit(boolean b) Shrinks the text to fit the cell width (true) instead of letting it overflow.strikethrough(boolean b) Sets (true) or clears (false) strikethrough.Shorthand forfontVertical(SUBSCRIPT).Shorthand forfontVertical(SUPERSCRIPT).textRotation(int degrees) Sets the text rotation in the ECMA-376 §18.8.1 encoding: 0-90 rotates counter-clockwise by that many degrees, 91-180 rotates clockwise by (value - 90) degrees, and 255 stacks the characters vertically.Sets the underline style.Sets the vertical alignment.wrapText(boolean b) Wraps text onto several lines inside the cell (true).
-
Method Details
-
fontName
Sets the font family name, e.g."Arial". An empty name becomes"Calibri", not the workbook default font.- Parameters:
name- the font family name;nullmakesRange.setStylethrowAxyraException- Returns:
- this builder
-
fontSize
Sets the font size. The size is stored to 1/100 pt.- Parameters:
points- the size in points (1/72 inch); 0 or below becomes 11 pt- Returns:
- this builder
-
bold
Sets (true) or clears (false) bold. -
bold
Makes the font bold; shorthand forbold(true). -
italic
Sets (true) or clears (false) italic. -
italic
Makes the font italic; shorthand foritalic(true). -
strikethrough
Sets (true) or clears (false) strikethrough. -
underline
Sets the underline style.- Parameters:
u- the underline style;Underline.NONEremoves it- Returns:
- this builder
- Throws:
NullPointerException- ifuisnull
-
fontColor
Sets the font color. Any color kind is accepted: RGB, theme, indexed or automatic.- Parameters:
c- the color- Returns:
- this builder
- Throws:
NullPointerException- ifcisnull; passColor.auto()for the automatic color
-
fontVertical
Sets the font position: baseline, superscript or subscript.- Parameters:
v- the position- Returns:
- this builder
- Throws:
NullPointerException- ifvisnull
-
superscript
Shorthand forfontVertical(SUPERSCRIPT). -
subscript
Shorthand forfontVertical(SUBSCRIPT). -
fontCharset
Sets the font character set code (0 = ANSI, 1 = DEFAULT, 128 = Shift-JIS, …).- Parameters:
charset- the code, 0-255; a value outside it makesRange.setStylethrowAxyraException- Returns:
- this builder
-
fontFamily
Sets the font family class: 0 = not applicable, 1 = Roman, 2 = Swiss, 3 = Modern, 4 = Script, 5 = Decorative.- Parameters:
family- the class, 0-255; a value outside it makesRange.setStylethrowAxyraException- Returns:
- this builder
-
fontScheme
Sets the theme font scheme the font belongs to.- Parameters:
scheme-"major","minor", or an empty string for a font that is not a theme font;nullmakesRange.setStylethrowAxyraException- Returns:
- this builder
-
fill
Sets a solid fill incolor: the pattern becomesPatternType.SOLIDandcolorits foreground. Any color kind is accepted.This does not remove a gradient. On a builder from
CellStyle.toBuilder()whose style has a gradient fill, the gradient stays and is what the cell shows and saves. Build the style fromCellStyle.builder()to replace a gradient.- Parameters:
color- the fill color- Returns:
- this builder
- Throws:
NullPointerException- ifcolorisnull; passColor.auto()for the automatic color
-
fillPattern
Sets a patterned fill:patterndrawn inforegroundoverbackground. ForPatternType.SOLID,foregroundis the visible fill andbackgroundis not drawn.Like
fill(Color), this does not remove a gradient that a builder fromCellStyle.toBuilder()carries; the gradient stays and wins.- Parameters:
pattern- the pattern;PatternType.NONEremoves the fillforeground- the pattern color;nullkeeps the builder's current foreground (automatic on a new builder)background- the color behind the pattern;nullkeeps the builder's current background (automatic on a new builder)- Returns:
- this builder
- Throws:
NullPointerException- ifpatternisnull
-
gradientFill
Sets a linear gradient fill acrosscolors, spread evenly from position 0 to 1. Pass two or more colors; one color gives a single stop at 0. The gradient replaces any pattern fill in the saved file. Read it back withCellStyle.gradientStops().- Parameters:
degree- the rotation in whole degrees, 0-359: 0 runs left to right, 90 top to bottom; a negative value makesRange.setStylethrowAxyraExceptioncolors- the stop colors, RGB only- Returns:
- this builder
- Throws:
IllegalStateException- if a color is a theme, indexed or automatic color
-
pathGradientFill
Sets a path gradient fill that converges at the cell center (Excel's "From center" gradient), acrosscolorsspread evenly from position 0 to 1. The first color sits at the center. Pass two or more colors. For a linear gradient usegradientFill(int, Color...). The gradient replaces any pattern fill in the saved file.- Parameters:
colors- the stop colors, RGB only- Returns:
- this builder
- Throws:
IllegalStateException- if a color is a theme, indexed or automatic color
-
border
Sets one border edge. ForSide.DIAGONAL, when neitherdiagonalDown(boolean)nordiagonalUp(boolean)was called before, the line runs from top-left to bottom-right; calldiagonalUp(true)for the other direction.- Parameters:
side- the edgestyle- the line style;BorderStyle.NONEremoves the linecolor- the line color; any color kind is accepted- Returns:
- this builder
- Throws:
NullPointerException- ifsideorstyleisnull, orcolorisnull; passColor.auto()for the automatic color
-
diagonalDown
Shows (true) or hides the diagonal line from top-left to bottom-right. -
diagonalUp
Shows (true) or hides the diagonal line from bottom-left to top-right. -
allBorders
Sets the left, right, top and bottom edges of each cell to the same line. Every cell of a range gets all four edges; for a box around the whole range, set the edges per side on the outer cells. The diagonal is not set.- Parameters:
style- the line stylecolor- the line color; any color kind is accepted- Returns:
- this builder
- Throws:
NullPointerException- ifstyleisnull, orcolorisnull; passColor.auto()for the automatic color
-
horizontalAlign
Sets the horizontal alignment.- Parameters:
h- the alignment- Returns:
- this builder
- Throws:
NullPointerException- ifhisnull
-
verticalAlign
Sets the vertical alignment.- Parameters:
v- the alignment- Returns:
- this builder
- Throws:
NullPointerException- ifvisnull
-
shrinkToFit
Shrinks the text to fit the cell width (true) instead of letting it overflow. -
wrapText
Wraps text onto several lines inside the cell (true). -
textRotation
Sets the text rotation in the ECMA-376 §18.8.1 encoding: 0-90 rotates counter-clockwise by that many degrees, 91-180 rotates clockwise by (value - 90) degrees, and 255 stacks the characters vertically. Pass 135 for -45 degrees. The value is not clamped.- Parameters:
degrees- the encoded rotation; a negative value makesRange.setStylethrowAxyraException- Returns:
- this builder
-
indent
Sets the indent level. Each level shifts the text by about one character width of the cell's font. It applies to left, right and distributed horizontal alignment. The value is not clamped.- Parameters:
level- the indent level; a negative value makesRange.setStylethrowAxyraException- Returns:
- this builder
-
numberFormat
Sets the Excel number format code, e.g."#,##0.00"or"yyyy-mm-dd". Codes use the en-US separators:.for decimals and,for thousands.- Parameters:
code- the format code; an empty code means General, andnullmakesRange.setStylethrowAxyraException- Returns:
- this builder
-
locked
Locks (true, the default) or unlocks the cell for sheet protection. -
build
Builds an immutable style; later builder calls do not change it.- Returns:
- the style
-