style.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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: *DefaulFont(),
  19. Border: *DefaulBorder(),
  20. Fill: *DefaulFill(),
  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{Style: style.Border.Left}
  56. xBorder.Right = xlsxLine{Style: style.Border.Right}
  57. xBorder.Top = xlsxLine{Style: style.Border.Top}
  58. xBorder.Bottom = xlsxLine{Style: style.Border.Bottom}
  59. xCellXf = makeXLSXCellElement()
  60. xCellXf.ApplyBorder = style.ApplyBorder
  61. xCellXf.ApplyFill = style.ApplyFill
  62. xCellXf.ApplyFont = style.ApplyFont
  63. xCellXf.ApplyAlignment = style.ApplyAlignment
  64. xCellStyleXf.ApplyBorder = style.ApplyBorder
  65. xCellStyleXf.ApplyFill = style.ApplyFill
  66. xCellStyleXf.ApplyFont = style.ApplyFont
  67. xCellStyleXf.ApplyAlignment = style.ApplyAlignment
  68. xCellStyleXf.NumFmtId = 0
  69. xCellStyleXf.Alignment = xlsxAlignment{Horizontal: style.Alignment.Horizontal, Vertical: style.Alignment.Vertical}
  70. return
  71. }
  72. func makeXLSXCellElement() (xCellXf xlsxXf) {
  73. xCellXf.NumFmtId = 0
  74. return
  75. }
  76. // Border is a high level structure intended to provide user access to
  77. // the contents of Border Style within an Sheet.
  78. type Border struct {
  79. Left string
  80. Right string
  81. Top string
  82. Bottom string
  83. }
  84. func NewBorder(left, right, top, bottom string) *Border {
  85. return &Border{Left: left, Right: right, Top: top, Bottom: bottom}
  86. }
  87. // Fill is a high level structure intended to provide user access to
  88. // the contents of background and foreground color index within an Sheet.
  89. type Fill struct {
  90. PatternType string
  91. BgColor string
  92. FgColor string
  93. }
  94. func NewFill(patternType, fgColor, bgColor string) *Fill {
  95. return &Fill{PatternType: patternType, FgColor: fgColor, BgColor: bgColor}
  96. }
  97. type Font struct {
  98. Size int
  99. Name string
  100. Family int
  101. Charset int
  102. Color string
  103. Bold bool
  104. Italic bool
  105. Underline bool
  106. }
  107. func NewFont(size int, name string) *Font {
  108. return &Font{Size: size, Name: name}
  109. }
  110. type Alignment struct {
  111. Horizontal string
  112. Vertical string
  113. }
  114. func DefaulFont() *Font {
  115. return NewFont(12, "Verdana")
  116. }
  117. func DefaulFill() *Fill {
  118. return NewFill("none", "FFFFFFFF", "00000000")
  119. }
  120. func DefaulBorder() *Border {
  121. return NewBorder("none", "none", "none", "none")
  122. }