日历

使用 Go 语言在 Excel 文档中创建日历:

使用 Go 语言在 Excel 文档中创建日历

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/360EntSecGroup-skylar/excelize"
  6. )
  7. func main() {
  8. // 创建工作簿
  9. f := excelize.NewFile()
  10. var (
  11. monthStyle, titleStyle, dataStyle, blankStyle,
  12. grayBlankStyle, grayDataStyle, noteStyle, noteLineStyle int
  13. err error
  14. addr string
  15. sheet = "Sheet1"
  16. // 定义单元格的值
  17. data = map[int][]interface{}{
  18. 1: {"五月 2020"},
  19. 3: {"日", "一", "二", "三",
  20. "四", "五", "六"},
  21. 4: {26, 27, 28, 29, 30, 1, 2},
  22. 6: {3, 4, 5, 6, 7, 8, 9},
  23. 8: {10, 11, 12, 13, 14, 15, 16},
  24. 10: {17, 18, 19, 20, 21, 22, 23},
  25. 12: {24, 25, 26, 27, 28, 29, 30},
  26. 14: {31, 1, 2, 3, 4, 5, 6},
  27. 18: {"笔记"},
  28. }
  29. // 自定义行高
  30. height = map[int]float64{
  31. 1: 45, 3: 22, 5: 44, 7: 44, 9: 44, 11: 44, 13: 44, 15: 44,
  32. 18: 24, 19: 24, 20: 24, 21: 24, 22: 24, 23: 24, 24: 24,
  33. }
  34. top = excelize.Border{Type: "top", Style: 1, Color: "DADEE0"}
  35. left = excelize.Border{Type: "left", Style: 1, Color: "DADEE0"}
  36. right = excelize.Border{Type: "right", Style: 1, Color: "DADEE0"}
  37. bottom = excelize.Border{Type: "bottom", Style: 1, Color: "DADEE0"}
  38. fill = excelize.Fill{Type: "pattern", Color: []string{"EFEFEF"}, Pattern: 1}
  39. )
  40. // 按行赋值
  41. for r, row := range data {
  42. if addr, err = excelize.JoinCellName("B", r); err != nil {
  43. fmt.Println(err)
  44. return
  45. }
  46. if err = f.SetSheetRow(sheet, addr, &row); err != nil {
  47. fmt.Println(err)
  48. return
  49. }
  50. }
  51. // 设置自定义行高
  52. for r, ht := range height {
  53. if err = f.SetRowHeight(sheet, r, ht); err != nil {
  54. fmt.Println(err)
  55. return
  56. }
  57. }
  58. // 设置列宽
  59. if err = f.SetColWidth(sheet, "B", "H", 16.5); err != nil {
  60. fmt.Println(err)
  61. return
  62. }
  63. // 合并月份单元格
  64. if err = f.MergeCell(sheet, "B1", "D1"); err != nil {
  65. fmt.Println(err)
  66. return
  67. }
  68. // 设置月份单元格样式
  69. if monthStyle, err = f.NewStyle(&excelize.Style{
  70. Font: &excelize.Font{Color: "1f7f3b", Bold: true, Size: 22, Family: "Microsoft YaHei"},
  71. }); err != nil {
  72. fmt.Println(err)
  73. return
  74. }
  75. // 设置月份单元格字体
  76. if err = f.SetCellStyle(sheet, "B1", "D1", monthStyle); err != nil {
  77. fmt.Println(err)
  78. return
  79. }
  80. // 创建周一至周日标题行样式
  81. if titleStyle, err = f.NewStyle(&excelize.Style{
  82. Font: &excelize.Font{Color: "1f7f3b", Bold: true, Family: "Microsoft YaHei"},
  83. Fill: excelize.Fill{Type: "pattern", Color: []string{"E6F4EA"}, Pattern: 1},
  84. Alignment: &excelize.Alignment{Vertical: "center", Horizontal: "center"},
  85. Border: []excelize.Border{{Type: "top", Style: 2, Color: "1f7f3b"}},
  86. }); err != nil {
  87. fmt.Println(err)
  88. return
  89. }
  90. // 设置周一至周日标题行样式
  91. if err = f.SetCellStyle(sheet, "B3", "H3", titleStyle); err != nil {
  92. fmt.Println(err)
  93. return
  94. }
  95. // 创建日期单元格样式
  96. if dataStyle, err = f.NewStyle(&excelize.Style{
  97. Border: []excelize.Border{top, left, right},
  98. }); err != nil {
  99. fmt.Println(err)
  100. return
  101. }
  102. // 设置日期单元格样式
  103. for _, r := range []int{4, 6, 8, 10, 12, 14} {
  104. if err = f.SetCellStyle(sheet, "B"+strconv.Itoa(r),
  105. "H"+strconv.Itoa(r), dataStyle); err != nil {
  106. fmt.Println(err)
  107. return
  108. }
  109. }
  110. // 创建空白单元格样式
  111. if blankStyle, err = f.NewStyle(&excelize.Style{
  112. Border: []excelize.Border{left, right, bottom},
  113. }); err != nil {
  114. fmt.Println(err)
  115. return
  116. }
  117. // 设置空白单元格样式
  118. for _, r := range []int{5, 7, 9, 11, 13, 15} {
  119. if err = f.SetCellStyle(sheet, "B"+strconv.Itoa(r),
  120. "H"+strconv.Itoa(r), blankStyle); err != nil {
  121. fmt.Println(err)
  122. return
  123. }
  124. }
  125. // 创建上个月和下个月日期中的空白单元格样式
  126. if grayBlankStyle, err = f.NewStyle(&excelize.Style{
  127. Border: []excelize.Border{left, right, bottom},
  128. Fill: fill}); err != nil {
  129. fmt.Println(err)
  130. return
  131. }
  132. // 设置上个月和下个月日期中的空白单元格样式
  133. if err = f.SetCellStyle(sheet, "B5", "F5", grayBlankStyle); err != nil {
  134. fmt.Println(err)
  135. return
  136. }
  137. if err = f.SetCellStyle(sheet, "C15", "H15", grayBlankStyle); err != nil {
  138. fmt.Println(err)
  139. return
  140. }
  141. // 创建上个月和下个月日期的单元格样式
  142. if grayDataStyle, err = f.NewStyle(&excelize.Style{
  143. Border: []excelize.Border{left, right, top},
  144. Font: &excelize.Font{Color: "777777"}, Fill: fill}); err != nil {
  145. fmt.Println(err)
  146. return
  147. }
  148. // 设置上个月和下个月日期的单元格样式
  149. if err = f.SetCellStyle(sheet, "B4", "F4", grayDataStyle); err != nil {
  150. fmt.Println(err)
  151. return
  152. }
  153. if err = f.SetCellStyle(sheet, "C14", "H14", grayDataStyle); err != nil {
  154. fmt.Println(err)
  155. return
  156. }
  157. // 创建笔记单元格样式
  158. if noteStyle, err = f.NewStyle(&excelize.Style{
  159. Font: &excelize.Font{Color: "1f7f3b", Bold: true, Size: 14, Family: "Microsoft YaHei"},
  160. }); err != nil {
  161. fmt.Println(err)
  162. return
  163. }
  164. // 设置笔记单元格样式
  165. if err = f.SetCellStyle(sheet, "B18", "B18", noteStyle); err != nil {
  166. fmt.Println(err)
  167. return
  168. }
  169. // 创建笔记区域横线样式
  170. if noteLineStyle, err = f.NewStyle(&excelize.Style{
  171. Border: []excelize.Border{{Type: "bottom", Style: 4, Color: "DDDDDD"}},
  172. }); err != nil {
  173. fmt.Println(err)
  174. return
  175. }
  176. // 设置笔记区域横线样式
  177. for r := 19; r < 25; r++ {
  178. if err = f.SetCellStyle(sheet, "B"+strconv.Itoa(r),
  179. "H"+strconv.Itoa(r), noteLineStyle); err != nil {
  180. fmt.Println(err)
  181. return
  182. }
  183. }
  184. // 隐藏工作表网格线
  185. if err = f.SetSheetViewOptions(sheet, 0,
  186. excelize.ShowGridLines(false)); err != nil {
  187. fmt.Println(err)
  188. return
  189. }
  190. // 重命名工作表
  191. f.SetSheetName(sheet, "五月 20")
  192. // 保存工作簿
  193. if err = f.SaveAs("日历.xlsx"); err != nil {
  194. fmt.Println(err)
  195. }
  196. }