Styling
Style Structure
A Style has five optional components:
style := &werkbook.Style{
Font: &werkbook.Font{...},
Fill: &werkbook.Fill{...},
Border: &werkbook.Border{...},
Alignment: &werkbook.Alignment{...},
NumFmt: "...", // or NumFmtID
}
Styles are whole objects. Calling SetStyle replaces the cell’s style pointer with the value you pass. If you want to preserve part of an existing style, read it first, modify it, then write it back.
style, _ := sheet.GetStyle("A1")
if style == nil {
style = &werkbook.Style{}
}
style.Font = &werkbook.Font{Bold: true}
sheet.SetStyle("A1", style)
Fonts
sheet.SetStyle("A1", &werkbook.Style{
Font: &werkbook.Font{
Name: "Calibri",
Size: 14,
Bold: true,
Italic: false,
Underline: true,
Color: "FF0000", // red, 6-char RGB hex
},
})
Fill Colors
Set a solid background fill:
sheet.SetStyle("B2", &werkbook.Style{
Fill: &werkbook.Fill{
Color: "FFFF00", // yellow
},
})
Colors are 6-character RGB hex strings (without the # prefix).
Borders
Each side of a cell can have its own style and color:
sheet.SetStyle("C3", &werkbook.Style{
Border: &werkbook.Border{
Top: werkbook.BorderSide{Style: werkbook.BorderThin, Color: "000000"},
Bottom: werkbook.BorderSide{Style: werkbook.BorderMedium, Color: "000000"},
Left: werkbook.BorderSide{Style: werkbook.BorderThin, Color: "CCCCCC"},
Right: werkbook.BorderSide{Style: werkbook.BorderThin, Color: "CCCCCC"},
},
})
Available border styles:
| Constant | Style |
|---|---|
BorderNone | No border |
BorderThin | Thin line |
BorderMedium | Medium line |
BorderThick | Thick line |
BorderDashed | Dashed line |
BorderDotted | Dotted line |
BorderDouble | Double line |
Alignment
sheet.SetStyle("D4", &werkbook.Style{
Alignment: &werkbook.Alignment{
Horizontal: werkbook.HAlignCenter,
Vertical: werkbook.VAlignTop,
WrapText: true,
},
})
Horizontal options: HAlignGeneral, HAlignLeft, HAlignCenter, HAlignRight
Vertical options: VAlignBottom, VAlignCenter, VAlignTop
Number Formats
Apply custom number formats using spreadsheet number format strings:
// Currency
sheet.SetStyle("E1", &werkbook.Style{NumFmt: "$#,##0.00"})
// Percentage
sheet.SetStyle("E2", &werkbook.Style{NumFmt: "0.00%"})
// Date
sheet.SetStyle("E3", &werkbook.Style{NumFmt: "yyyy-mm-dd"})
// Thousands separator
sheet.SetStyle("E4", &werkbook.Style{NumFmt: "#,##0"})
You can also use built-in format IDs with NumFmtID (0–163).
Range Styles
Apply a style to an entire range at once:
headerStyle := &werkbook.Style{
Font: &werkbook.Font{Bold: true, Color: "FFFFFF"},
Fill: &werkbook.Fill{Color: "333333"},
Alignment: &werkbook.Alignment{Horizontal: werkbook.HAlignCenter},
}
sheet.SetRangeStyle("A1:D1", headerStyle)
SetRangeStyle creates any missing cells in the range so the style has somewhere to live.
Reading Styles
style, _ := sheet.GetStyle("A1")
if style != nil && style.Font != nil {
fmt.Printf("Font: %s %.0fpt\n", style.Font.Name, style.Font.Size)
}
GetStyle returns nil for default-styled or nonexistent cells.
Full Example
// Style a header row
sheet.SetRangeStyle("A1:E1", &werkbook.Style{
Font: &werkbook.Font{Bold: true, Size: 12, Color: "FFFFFF"},
Fill: &werkbook.Fill{Color: "2B579A"},
Alignment: &werkbook.Alignment{Horizontal: werkbook.HAlignCenter},
Border: &werkbook.Border{
Bottom: werkbook.BorderSide{
Style: werkbook.BorderMedium,
Color: "1A3A6B",
},
},
})
// Style currency column
for row := 2; row <= 10; row++ {
cell := fmt.Sprintf("D%d", row)
sheet.SetStyle(cell, &werkbook.Style{
NumFmt: "$#,##0.00",
Alignment: &werkbook.Alignment{Horizontal: werkbook.HAlignRight},
})
}