Class CellStyle.Builder

java.lang.Object
io.keikai.axyra.sheets.style.CellStyle.Builder
Enclosing class:
CellStyle

public static final class CellStyle.Builder extends Object
Fluent builder for a 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 Details

    • fontName

      public CellStyle.Builder fontName(String name)
      Sets the font family name, e.g. "Arial". An empty name becomes "Calibri", not the workbook default font.
      Parameters:
      name - the font family name; null makes Range.setStyle throw AxyraException
      Returns:
      this builder
    • fontSize

      public CellStyle.Builder fontSize(double points)
      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

      public CellStyle.Builder bold(boolean b)
      Sets (true) or clears (false) bold.
    • bold

      public CellStyle.Builder bold()
      Makes the font bold; shorthand for bold(true).
    • italic

      public CellStyle.Builder italic(boolean b)
      Sets (true) or clears (false) italic.
    • italic

      public CellStyle.Builder italic()
      Makes the font italic; shorthand for italic(true).
    • strikethrough

      public CellStyle.Builder strikethrough(boolean b)
      Sets (true) or clears (false) strikethrough.
    • underline

      public CellStyle.Builder underline(Underline u)
      Sets the underline style.
      Parameters:
      u - the underline style; Underline.NONE removes it
      Returns:
      this builder
      Throws:
      NullPointerException - if u is null
    • fontColor

      public CellStyle.Builder fontColor(Color c)
      Sets the font color. Any color kind is accepted: RGB, theme, indexed or automatic.
      Parameters:
      c - the color
      Returns:
      this builder
      Throws:
      NullPointerException - if c is null; pass Color.auto() for the automatic color
    • fontVertical

      public CellStyle.Builder fontVertical(FontVertical v)
      Sets the font position: baseline, superscript or subscript.
      Parameters:
      v - the position
      Returns:
      this builder
      Throws:
      NullPointerException - if v is null
    • superscript

      public CellStyle.Builder superscript()
      Shorthand for fontVertical(SUPERSCRIPT).
    • subscript

      public CellStyle.Builder subscript()
      Shorthand for fontVertical(SUBSCRIPT).
    • fontCharset

      public CellStyle.Builder fontCharset(int charset)
      Sets the font character set code (0 = ANSI, 1 = DEFAULT, 128 = Shift-JIS, …).
      Parameters:
      charset - the code, 0-255; a value outside it makes Range.setStyle throw AxyraException
      Returns:
      this builder
    • fontFamily

      public CellStyle.Builder fontFamily(int family)
      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 makes Range.setStyle throw AxyraException
      Returns:
      this builder
    • fontScheme

      public CellStyle.Builder fontScheme(String scheme)
      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; null makes Range.setStyle throw AxyraException
      Returns:
      this builder
    • fill

      public CellStyle.Builder fill(Color color)
      Sets a solid fill in color: the pattern becomes PatternType.SOLID and color its 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 from CellStyle.builder() to replace a gradient.

      Parameters:
      color - the fill color
      Returns:
      this builder
      Throws:
      NullPointerException - if color is null; pass Color.auto() for the automatic color
    • fillPattern

      public CellStyle.Builder fillPattern(PatternType pattern, Color foreground, Color background)
      Sets a patterned fill: pattern drawn in foreground over background. For PatternType.SOLID, foreground is the visible fill and background is not drawn.

      Like fill(Color), this does not remove a gradient that a builder from CellStyle.toBuilder() carries; the gradient stays and wins.

      Parameters:
      pattern - the pattern; PatternType.NONE removes the fill
      foreground - the pattern color; null keeps the builder's current foreground (automatic on a new builder)
      background - the color behind the pattern; null keeps the builder's current background (automatic on a new builder)
      Returns:
      this builder
      Throws:
      NullPointerException - if pattern is null
    • gradientFill

      public CellStyle.Builder gradientFill(int degree, Color... colors)
      Sets a linear gradient fill across colors, 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 with CellStyle.gradientStops().
      Parameters:
      degree - the rotation in whole degrees, 0-359: 0 runs left to right, 90 top to bottom; a negative value makes Range.setStyle throw AxyraException
      colors - the stop colors, RGB only
      Returns:
      this builder
      Throws:
      IllegalStateException - if a color is a theme, indexed or automatic color
    • pathGradientFill

      public CellStyle.Builder pathGradientFill(Color... colors)
      Sets a path gradient fill that converges at the cell center (Excel's "From center" gradient), across colors spread evenly from position 0 to 1. The first color sits at the center. Pass two or more colors. For a linear gradient use gradientFill(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

      public CellStyle.Builder border(Side side, BorderStyle style, Color color)
      Sets one border edge. For Side.DIAGONAL, when neither diagonalDown(boolean) nor diagonalUp(boolean) was called before, the line runs from top-left to bottom-right; call diagonalUp(true) for the other direction.
      Parameters:
      side - the edge
      style - the line style; BorderStyle.NONE removes the line
      color - the line color; any color kind is accepted
      Returns:
      this builder
      Throws:
      NullPointerException - if side or style is null, or color is null; pass Color.auto() for the automatic color
    • diagonalDown

      public CellStyle.Builder diagonalDown(boolean on)
      Shows (true) or hides the diagonal line from top-left to bottom-right.
    • diagonalUp

      public CellStyle.Builder diagonalUp(boolean on)
      Shows (true) or hides the diagonal line from bottom-left to top-right.
    • allBorders

      public CellStyle.Builder allBorders(BorderStyle style, Color color)
      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 style
      color - the line color; any color kind is accepted
      Returns:
      this builder
      Throws:
      NullPointerException - if style is null, or color is null; pass Color.auto() for the automatic color
    • horizontalAlign

      public CellStyle.Builder horizontalAlign(HAlign h)
      Sets the horizontal alignment.
      Parameters:
      h - the alignment
      Returns:
      this builder
      Throws:
      NullPointerException - if h is null
    • verticalAlign

      public CellStyle.Builder verticalAlign(VAlign v)
      Sets the vertical alignment.
      Parameters:
      v - the alignment
      Returns:
      this builder
      Throws:
      NullPointerException - if v is null
    • shrinkToFit

      public CellStyle.Builder shrinkToFit(boolean b)
      Shrinks the text to fit the cell width (true) instead of letting it overflow.
    • wrapText

      public CellStyle.Builder wrapText(boolean b)
      Wraps text onto several lines inside the cell (true).
    • textRotation

      public CellStyle.Builder 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. Pass 135 for -45 degrees. The value is not clamped.
      Parameters:
      degrees - the encoded rotation; a negative value makes Range.setStyle throw AxyraException
      Returns:
      this builder
    • indent

      public CellStyle.Builder indent(int level)
      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 makes Range.setStyle throw AxyraException
      Returns:
      this builder
    • numberFormat

      public CellStyle.Builder numberFormat(String code)
      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, and null makes Range.setStyle throw AxyraException
      Returns:
      this builder
    • locked

      public CellStyle.Builder locked(boolean b)
      Locks (true, the default) or unlocks the cell for sheet protection.
    • hidden

      public CellStyle.Builder hidden(boolean b)
      Hides the formula from the formula bar while the sheet is protected (true).
    • build

      public CellStyle build()
      Builds an immutable style; later builder calls do not change it.
      Returns:
      the style