style.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package excelutil
  2. import "github.com/xuri/excelize/v2"
  3. type style struct {
  4. file *excelize.File
  5. }
  6. func NewStyle(file *excelize.File) *style {
  7. return &style{
  8. file: file,
  9. }
  10. }
  11. func (s *style) NewBoldTitleStyleId() (int, error) {
  12. styleID, err := s.file.NewStyle(&excelize.Style{
  13. // 标题加粗
  14. Font: &excelize.Font{
  15. Bold: true,
  16. Color: "000000",
  17. },
  18. // 居中
  19. Alignment: &excelize.Alignment{
  20. // 水平对齐:
  21. Horizontal: "center",
  22. // 垂直对齐
  23. Vertical: "center",
  24. // 单元格内容过多则自动换行
  25. WrapText: true,
  26. },
  27. })
  28. return styleID, err
  29. }
  30. func (s *style) NewBoldBorderTitleStyleId() (int, error) {
  31. styleID, err := s.file.NewStyle(&excelize.Style{
  32. // 标题加粗
  33. Font: &excelize.Font{
  34. Bold: true,
  35. Color: "000000",
  36. },
  37. // 设置边框
  38. Border: []excelize.Border{
  39. {Type: "left", Color: "000000", Style: 1},
  40. {Type: "top", Color: "000000", Style: 1},
  41. {Type: "bottom", Color: "000000", Style: 1},
  42. {Type: "right", Color: "000000", Style: 1},
  43. },
  44. // 居中
  45. Alignment: &excelize.Alignment{
  46. // 水平对齐:
  47. Horizontal: "center",
  48. // 垂直对齐
  49. Vertical: "center",
  50. // 单元格内容过多则自动换行
  51. WrapText: true,
  52. },
  53. })
  54. return styleID, err
  55. }
  56. func (s *style) NewGreenBoldBorderTitleStyleId() (int, error) {
  57. styleID, err := s.file.NewStyle(&excelize.Style{
  58. // 标题加粗
  59. Font: &excelize.Font{
  60. Bold: true,
  61. Color: "FFFFFF",
  62. },
  63. // 设置边框
  64. Border: []excelize.Border{
  65. {Type: "left", Color: "000000", Style: 1},
  66. {Type: "top", Color: "000000", Style: 1},
  67. {Type: "bottom", Color: "000000", Style: 1},
  68. {Type: "right", Color: "000000", Style: 1},
  69. },
  70. // 图案填充-绿色
  71. Fill: excelize.Fill{
  72. Pattern: 1,
  73. Type: "pattern",
  74. Color: []string{"2EA121"},
  75. },
  76. // 居中
  77. Alignment: &excelize.Alignment{
  78. // 水平对齐:
  79. Horizontal: "center",
  80. // 垂直对齐
  81. Vertical: "center",
  82. // 单元格内容过多则自动换行
  83. WrapText: true,
  84. },
  85. })
  86. return styleID, err
  87. }
  88. func (s *style) NewYellowBoldBorderTitleStyleId() (int, error) {
  89. styleID, err := s.file.NewStyle(&excelize.Style{
  90. // 标题加粗
  91. Font: &excelize.Font{
  92. Bold: true,
  93. Color: "FFFFFF",
  94. },
  95. // 设置边框
  96. Border: []excelize.Border{
  97. {Type: "left", Color: "000000", Style: 1},
  98. {Type: "top", Color: "000000", Style: 1},
  99. {Type: "bottom", Color: "000000", Style: 1},
  100. {Type: "right", Color: "000000", Style: 1},
  101. },
  102. // 图案填充-黄色
  103. Fill: excelize.Fill{
  104. Pattern: 1,
  105. Type: "pattern",
  106. Color: []string{"FFC60A"},
  107. },
  108. // 居中
  109. Alignment: &excelize.Alignment{
  110. // 水平对齐:
  111. Horizontal: "center",
  112. // 垂直对齐
  113. Vertical: "center",
  114. // 单元格内容过多则自动换行
  115. WrapText: true,
  116. },
  117. })
  118. return styleID, err
  119. }
  120. func (s *style) NewDefaultValueStyleId() (int, error) {
  121. styleID, err := s.file.NewStyle(&excelize.Style{
  122. // 居中
  123. Alignment: &excelize.Alignment{
  124. // 水平对齐:
  125. Horizontal: "center",
  126. // 垂直对齐
  127. Vertical: "center",
  128. },
  129. })
  130. return styleID, err
  131. }
  132. func (s *style) NewDefaultValueStyleIdWithBorder() (int, error) {
  133. styleID, err := s.file.NewStyle(&excelize.Style{
  134. // 设置边框
  135. Border: []excelize.Border{
  136. {Type: "left", Color: "000000", Style: 1},
  137. {Type: "top", Color: "000000", Style: 1},
  138. {Type: "bottom", Color: "000000", Style: 1},
  139. {Type: "right", Color: "000000", Style: 1},
  140. },
  141. // 居中
  142. Alignment: &excelize.Alignment{
  143. // 水平对齐:
  144. Horizontal: "center",
  145. // 垂直对齐
  146. Vertical: "center",
  147. },
  148. })
  149. return styleID, err
  150. }
  151. func (s *style) NewDefaultTimeStyleId() (int, error) {
  152. styleID, err := s.file.NewStyle(&excelize.Style{
  153. // 居中
  154. Alignment: &excelize.Alignment{
  155. // 水平对齐:
  156. Horizontal: "center",
  157. // 垂直对齐
  158. Vertical: "center",
  159. },
  160. NumFmt: 22,
  161. })
  162. return styleID, err
  163. }
  164. func (s *style) NewDefaultTimeStyleIdWithBorder() (int, error) {
  165. styleID, err := s.file.NewStyle(&excelize.Style{
  166. // 设置边框
  167. Border: []excelize.Border{
  168. {Type: "left", Color: "000000", Style: 1},
  169. {Type: "top", Color: "000000", Style: 1},
  170. {Type: "bottom", Color: "000000", Style: 1},
  171. {Type: "right", Color: "000000", Style: 1},
  172. },
  173. // 居中
  174. Alignment: &excelize.Alignment{
  175. // 水平对齐:
  176. Horizontal: "center",
  177. // 垂直对齐
  178. Vertical: "center",
  179. },
  180. NumFmt: 22,
  181. })
  182. return styleID, err
  183. }