style.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package xlsx
  2. import "strconv"
  3. // Several popular font names that can be used to create fonts
  4. const (
  5. Helvetica = "Helvetica"
  6. Baskerville = "Baskerville Old Face"
  7. TimesNewRoman = "Times New Roman"
  8. Bodoni = "Bodoni MT"
  9. GillSans = "Gill Sans MT"
  10. Courier = "Courier"
  11. )
  12. const (
  13. RGB_Light_Green = "FFC6EFCE"
  14. RGB_Dark_Green = "FF006100"
  15. RGB_Light_Red = "FFFFC7CE"
  16. RGB_Dark_Red = "FF9C0006"
  17. RGB_White = "00000000"
  18. RGB_Black = "FFFFFFFF"
  19. )
  20. const (
  21. Solid_Cell_Fill = "solid"
  22. )
  23. // Style is a high level structure intended to provide user access to
  24. // the contents of Style within an XLSX file.
  25. type Style struct {
  26. Border Border
  27. Fill Fill
  28. Font Font
  29. ApplyBorder bool
  30. ApplyFill bool
  31. ApplyFont bool
  32. ApplyAlignment bool
  33. Alignment Alignment
  34. NamedStyleIndex *int
  35. }
  36. // Return a new Style structure initialised with the default values.
  37. func NewStyle() *Style {
  38. return &Style{
  39. Alignment: *DefaultAlignment(),
  40. Border: *DefaultBorder(),
  41. Fill: *DefaultFill(),
  42. Font: *DefaultFont(),
  43. }
  44. }
  45. // Generate the underlying XLSX style elements that correspond to the Style.
  46. func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBorder xlsxBorder, xCellXf xlsxXf) {
  47. xFont = xlsxFont{}
  48. xFill = xlsxFill{}
  49. xBorder = xlsxBorder{}
  50. xCellXf = xlsxXf{}
  51. xFont.Sz.Val = strconv.Itoa(style.Font.Size)
  52. xFont.Name.Val = style.Font.Name
  53. xFont.Family.Val = strconv.Itoa(style.Font.Family)
  54. xFont.Charset.Val = strconv.Itoa(style.Font.Charset)
  55. xFont.Color.RGB = style.Font.Color
  56. if style.Font.Bold {
  57. xFont.B = &xlsxVal{}
  58. } else {
  59. xFont.B = nil
  60. }
  61. if style.Font.Italic {
  62. xFont.I = &xlsxVal{}
  63. } else {
  64. xFont.I = nil
  65. }
  66. if style.Font.Underline {
  67. xFont.U = &xlsxVal{}
  68. } else {
  69. xFont.U = nil
  70. }
  71. xPatternFill := xlsxPatternFill{}
  72. xPatternFill.PatternType = style.Fill.PatternType
  73. xPatternFill.FgColor.RGB = style.Fill.FgColor
  74. xPatternFill.BgColor.RGB = style.Fill.BgColor
  75. xFill.PatternFill = xPatternFill
  76. xBorder.Left = xlsxLine{
  77. Style: style.Border.Left,
  78. Color: xlsxColor{RGB: style.Border.LeftColor},
  79. }
  80. xBorder.Right = xlsxLine{
  81. Style: style.Border.Right,
  82. Color: xlsxColor{RGB: style.Border.RightColor},
  83. }
  84. xBorder.Top = xlsxLine{
  85. Style: style.Border.Top,
  86. Color: xlsxColor{RGB: style.Border.TopColor},
  87. }
  88. xBorder.Bottom = xlsxLine{
  89. Style: style.Border.Bottom,
  90. Color: xlsxColor{RGB: style.Border.BottomColor},
  91. }
  92. xCellXf = makeXLSXCellElement()
  93. xCellXf.ApplyBorder = style.ApplyBorder
  94. xCellXf.ApplyFill = style.ApplyFill
  95. xCellXf.ApplyFont = style.ApplyFont
  96. xCellXf.ApplyAlignment = style.ApplyAlignment
  97. if style.NamedStyleIndex != nil {
  98. xCellXf.XfId = style.NamedStyleIndex
  99. }
  100. return
  101. }
  102. func makeXLSXCellElement() (xCellXf xlsxXf) {
  103. xCellXf.NumFmtId = 0
  104. return
  105. }
  106. // Border is a high level structure intended to provide user access to
  107. // the contents of Border Style within an Sheet.
  108. type Border struct {
  109. Left string
  110. LeftColor string
  111. Right string
  112. RightColor string
  113. Top string
  114. TopColor string
  115. Bottom string
  116. BottomColor string
  117. }
  118. func NewBorder(left, right, top, bottom string) *Border {
  119. return &Border{
  120. Left: left,
  121. Right: right,
  122. Top: top,
  123. Bottom: bottom,
  124. }
  125. }
  126. // Fill is a high level structure intended to provide user access to
  127. // the contents of background and foreground color index within an Sheet.
  128. type Fill struct {
  129. PatternType string
  130. BgColor string
  131. FgColor string
  132. }
  133. func NewFill(patternType, fgColor, bgColor string) *Fill {
  134. return &Fill{
  135. PatternType: patternType,
  136. FgColor: fgColor,
  137. BgColor: bgColor,
  138. }
  139. }
  140. type Font struct {
  141. Size int
  142. Name string
  143. Family int
  144. Charset int
  145. Color string
  146. Bold bool
  147. Italic bool
  148. Underline bool
  149. }
  150. func NewFont(size int, name string) *Font {
  151. return &Font{Size: size, Name: name}
  152. }
  153. type Alignment struct {
  154. Horizontal string
  155. Indent int
  156. ShrinkToFit bool
  157. TextRotation int
  158. Vertical string
  159. WrapText bool
  160. }
  161. var defaultFontSize = 12
  162. var defaultFontName = "Verdana"
  163. func SetDefaultFont(size int, name string) {
  164. defaultFontSize = size
  165. defaultFontName = name
  166. }
  167. func DefaultFont() *Font {
  168. return NewFont(defaultFontSize, defaultFontName)
  169. }
  170. func DefaultFill() *Fill {
  171. return NewFill("none", "", "")
  172. }
  173. func DefaultBorder() *Border {
  174. return NewBorder("none", "none", "none", "none")
  175. }
  176. func DefaultAlignment() *Alignment {
  177. return &Alignment{
  178. Horizontal: "general",
  179. Vertical: "bottom",
  180. }
  181. }