style.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. ApplyAlignment bool
  13. Alignment Alignment
  14. }
  15. // Return a new Style structure initialised with the default values.
  16. func NewStyle() *Style {
  17. return &Style{
  18. Font: *DefaultFont(),
  19. Border: *DefaultBorder(),
  20. Fill: *DefaultFill(),
  21. }
  22. }
  23. // Generate the underlying XLSX style elements that correspond to the Style.
  24. func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBorder xlsxBorder, xCellStyleXf xlsxXf, xCellXf xlsxXf) {
  25. xFont = xlsxFont{}
  26. xFill = xlsxFill{}
  27. xBorder = xlsxBorder{}
  28. xCellStyleXf = xlsxXf{}
  29. xCellXf = xlsxXf{}
  30. xFont.Sz.Val = strconv.Itoa(style.Font.Size)
  31. xFont.Name.Val = style.Font.Name
  32. xFont.Family.Val = strconv.Itoa(style.Font.Family)
  33. xFont.Charset.Val = strconv.Itoa(style.Font.Charset)
  34. xFont.Color.RGB = style.Font.Color
  35. if style.Font.Bold {
  36. xFont.B = &xlsxVal{}
  37. } else {
  38. xFont.B = nil
  39. }
  40. if style.Font.Italic {
  41. xFont.I = &xlsxVal{}
  42. } else {
  43. xFont.I = nil
  44. }
  45. if style.Font.Underline {
  46. xFont.U = &xlsxVal{}
  47. } else {
  48. xFont.U = nil
  49. }
  50. xPatternFill := xlsxPatternFill{}
  51. xPatternFill.PatternType = style.Fill.PatternType
  52. xPatternFill.FgColor.RGB = style.Fill.FgColor
  53. xPatternFill.BgColor.RGB = style.Fill.BgColor
  54. xFill.PatternFill = xPatternFill
  55. xBorder.Left = xlsxLine{
  56. Style: style.Border.Left,
  57. Color: xlsxColor{RGB: style.Border.LeftColor},
  58. }
  59. xBorder.Right = xlsxLine{
  60. Style: style.Border.Right,
  61. Color: xlsxColor{RGB: style.Border.RightColor},
  62. }
  63. xBorder.Top = xlsxLine{
  64. Style: style.Border.Top,
  65. Color: xlsxColor{RGB: style.Border.TopColor},
  66. }
  67. xBorder.Bottom = xlsxLine{
  68. Style: style.Border.Bottom,
  69. Color: xlsxColor{RGB: style.Border.BottomColor},
  70. }
  71. xCellXf = makeXLSXCellElement()
  72. xCellXf.ApplyBorder = style.ApplyBorder
  73. xCellXf.ApplyFill = style.ApplyFill
  74. xCellXf.ApplyFont = style.ApplyFont
  75. xCellXf.ApplyAlignment = style.ApplyAlignment
  76. xCellStyleXf.ApplyBorder = style.ApplyBorder
  77. xCellStyleXf.ApplyFill = style.ApplyFill
  78. xCellStyleXf.ApplyFont = style.ApplyFont
  79. xCellStyleXf.ApplyAlignment = style.ApplyAlignment
  80. xCellStyleXf.NumFmtId = 0
  81. xCellStyleXf.Alignment = xlsxAlignment{Horizontal: style.Alignment.Horizontal, Vertical: style.Alignment.Vertical}
  82. return
  83. }
  84. func makeXLSXCellElement() (xCellXf xlsxXf) {
  85. xCellXf.NumFmtId = 0
  86. return
  87. }
  88. // Border is a high level structure intended to provide user access to
  89. // the contents of Border Style within an Sheet.
  90. type Border struct {
  91. Left string
  92. LeftColor string
  93. Right string
  94. RightColor string
  95. Top string
  96. TopColor string
  97. Bottom string
  98. BottomColor string
  99. }
  100. func NewBorder(left, right, top, bottom string) *Border {
  101. return &Border{
  102. Left: left,
  103. LeftColor: "",
  104. Right: right,
  105. RightColor: "",
  106. Top: top,
  107. TopColor: "",
  108. Bottom: bottom,
  109. BottomColor: "",
  110. }
  111. }
  112. // Fill is a high level structure intended to provide user access to
  113. // the contents of background and foreground color index within an Sheet.
  114. type Fill struct {
  115. PatternType string
  116. BgColor string
  117. FgColor string
  118. }
  119. func NewFill(patternType, fgColor, bgColor string) *Fill {
  120. return &Fill{PatternType: patternType, FgColor: fgColor, BgColor: bgColor}
  121. }
  122. type Font struct {
  123. Size int
  124. Name string
  125. Family int
  126. Charset int
  127. Color string
  128. Bold bool
  129. Italic bool
  130. Underline bool
  131. }
  132. func NewFont(size int, name string) *Font {
  133. return &Font{Size: size, Name: name}
  134. }
  135. type Alignment struct {
  136. Horizontal string
  137. Vertical string
  138. }
  139. var defaultFontSize int
  140. var defaultFontName string
  141. func init() {
  142. defaultFontSize = 12
  143. defaultFontName = "Verdana"
  144. }
  145. func SetDefaultFont(size int, name string) {
  146. defaultFontSize = size
  147. defaultFontName = name
  148. }
  149. func DefaultFont() *Font {
  150. return NewFont(defaultFontSize, defaultFontName)
  151. }
  152. func DefaultFill() *Fill {
  153. return NewFill("none", "FFFFFFFF", "00000000")
  154. }
  155. func DefaultBorder() *Border {
  156. return NewBorder("none", "none", "none", "none")
  157. }