style.go 4.2 KB

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