style.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package xlsx
  2. import "strconv"
  3. // Style is a high level structure intended to provide user access to
  4. // the contents of Style within an XLSX file.
  5. type Style struct {
  6. Border Border
  7. Fill Fill
  8. Font Font
  9. ApplyBorder bool
  10. ApplyFill bool
  11. ApplyFont bool
  12. Alignment Alignment
  13. }
  14. // Return a new Style structure initialised with the default values.
  15. func NewStyle() *Style {
  16. return &Style{
  17. Font: *DefaulFont(),
  18. Border: *DefaulBorder(),
  19. Fill: *DefaulFill(),
  20. }
  21. }
  22. // Generate the underlying XLSX style elements that correspond to the Style.
  23. func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBorder xlsxBorder, xCellStyleXf xlsxXf, xCellXf xlsxXf) {
  24. xFont = xlsxFont{}
  25. xFill = xlsxFill{}
  26. xBorder = xlsxBorder{}
  27. xCellStyleXf = xlsxXf{}
  28. xCellXf = xlsxXf{}
  29. xFont.Sz.Val = strconv.Itoa(style.Font.Size)
  30. xFont.Name.Val = style.Font.Name
  31. xFont.Family.Val = strconv.Itoa(style.Font.Family)
  32. xFont.Charset.Val = strconv.Itoa(style.Font.Charset)
  33. xFont.Color.RGB = style.Font.Color
  34. xPatternFill := xlsxPatternFill{}
  35. xPatternFill.PatternType = style.Fill.PatternType
  36. xPatternFill.FgColor.RGB = style.Fill.FgColor
  37. xPatternFill.BgColor.RGB = style.Fill.BgColor
  38. xFill.PatternFill = xPatternFill
  39. xBorder.Left = xlsxLine{Style: style.Border.Left}
  40. xBorder.Right = xlsxLine{Style: style.Border.Right}
  41. xBorder.Top = xlsxLine{Style: style.Border.Top}
  42. xBorder.Bottom = xlsxLine{Style: style.Border.Bottom}
  43. xCellXf.ApplyBorder = style.ApplyBorder
  44. xCellXf.ApplyFill = style.ApplyFill
  45. xCellXf.ApplyFont = style.ApplyFont
  46. xCellXf.NumFmtId = 0
  47. xCellStyleXf.ApplyBorder = style.ApplyBorder
  48. xCellStyleXf.ApplyFill = style.ApplyFill
  49. xCellStyleXf.ApplyFont = style.ApplyFont
  50. xCellStyleXf.NumFmtId = 0
  51. return
  52. }
  53. // Border is a high level structure intended to provide user access to
  54. // the contents of Border Style within an Sheet.
  55. type Border struct {
  56. Left string
  57. Right string
  58. Top string
  59. Bottom string
  60. }
  61. func NewBorder(left, right, top, bottom string) *Border {
  62. return &Border{Left: left, Right: right, Top: top, Bottom: bottom}
  63. }
  64. // Fill is a high level structure intended to provide user access to
  65. // the contents of background and foreground color index within an Sheet.
  66. type Fill struct {
  67. PatternType string
  68. BgColor string
  69. FgColor string
  70. }
  71. func NewFill(patternType, fgColor, bgColor string) *Fill {
  72. return &Fill{PatternType: patternType, FgColor: fgColor, BgColor: bgColor}
  73. }
  74. type Font struct {
  75. Size int
  76. Name string
  77. Family int
  78. Charset int
  79. Color string
  80. Bold bool
  81. Italic bool
  82. Underline bool
  83. }
  84. func NewFont(size int, name string) *Font {
  85. return &Font{Size: size, Name: name}
  86. }
  87. type Alignment struct {
  88. Horizontal string
  89. }
  90. func DefaulFont() *Font {
  91. return NewFont(12, "Verdana")
  92. }
  93. func DefaulFill() *Fill {
  94. return NewFill("none", "FFFFFFFF", "00000000")
  95. }
  96. func DefaulBorder() *Border {
  97. return NewBorder("none", "none", "none", "none")
  98. }