style.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if style.Font.Bold {
  35. xFont.B = &struct{}{}
  36. } else {
  37. xFont.B = nil
  38. }
  39. if style.Font.Italic {
  40. xFont.I = &struct{}{}
  41. } else {
  42. xFont.I = nil
  43. }
  44. if style.Font.Underline {
  45. xFont.U = &struct{}{}
  46. } else {
  47. xFont.U = nil
  48. }
  49. xPatternFill := xlsxPatternFill{}
  50. xPatternFill.PatternType = style.Fill.PatternType
  51. xPatternFill.FgColor.RGB = style.Fill.FgColor
  52. xPatternFill.BgColor.RGB = style.Fill.BgColor
  53. xFill.PatternFill = xPatternFill
  54. xBorder.Left = xlsxLine{Style: style.Border.Left}
  55. xBorder.Right = xlsxLine{Style: style.Border.Right}
  56. xBorder.Top = xlsxLine{Style: style.Border.Top}
  57. xBorder.Bottom = xlsxLine{Style: style.Border.Bottom}
  58. xCellXf.ApplyBorder = style.ApplyBorder
  59. xCellXf.ApplyFill = style.ApplyFill
  60. xCellXf.ApplyFont = style.ApplyFont
  61. xCellXf.NumFmtId = 0
  62. xCellStyleXf.ApplyBorder = style.ApplyBorder
  63. xCellStyleXf.ApplyFill = style.ApplyFill
  64. xCellStyleXf.ApplyFont = style.ApplyFont
  65. xCellStyleXf.NumFmtId = 0
  66. return
  67. }
  68. // Border is a high level structure intended to provide user access to
  69. // the contents of Border Style within an Sheet.
  70. type Border struct {
  71. Left string
  72. Right string
  73. Top string
  74. Bottom string
  75. }
  76. func NewBorder(left, right, top, bottom string) *Border {
  77. return &Border{Left: left, Right: right, Top: top, Bottom: bottom}
  78. }
  79. // Fill is a high level structure intended to provide user access to
  80. // the contents of background and foreground color index within an Sheet.
  81. type Fill struct {
  82. PatternType string
  83. BgColor string
  84. FgColor string
  85. }
  86. func NewFill(patternType, fgColor, bgColor string) *Fill {
  87. return &Fill{PatternType: patternType, FgColor: fgColor, BgColor: bgColor}
  88. }
  89. type Font struct {
  90. Size int
  91. Name string
  92. Family int
  93. Charset int
  94. Color string
  95. Bold bool
  96. Italic bool
  97. Underline bool
  98. }
  99. func NewFont(size int, name string) *Font {
  100. return &Font{Size: size, Name: name}
  101. }
  102. type Alignment struct {
  103. Horizontal string
  104. }
  105. func DefaulFont() *Font {
  106. return NewFont(12, "Verdana")
  107. }
  108. func DefaulFill() *Fill {
  109. return NewFill("none", "FFFFFFFF", "00000000")
  110. }
  111. func DefaulBorder() *Border {
  112. return NewBorder("none", "none", "none", "none")
  113. }