级联列表

使用 Go 语言在 Excel 文档中创建多级联动菜单列表:

使用 Go 语言在 Excel 文档中创建多级联动菜单列表

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/360EntSecGroup-Skylar/excelize/v2"
  5. )
  6. func main() {
  7. // 创建工作簿
  8. f := excelize.NewFile()
  9. var (
  10. // 定义单元格的值
  11. data = [][]interface{}{
  12. {"Fruits", "Vegetables"},
  13. {"Mango", "Potato", nil, "Drop Down 1", "Drop Down 2"},
  14. {"Apple", "Tomato"},
  15. {"Grapes", "Spinach"},
  16. {"Strawberry", "Onion"},
  17. {"Kiwi", "Cucumber"},
  18. }
  19. addr string
  20. err error
  21. cellsStyle, headerStyle int
  22. )
  23. // 按行赋值
  24. for r, row := range data {
  25. if addr, err = excelize.JoinCellName("A", r+1); err != nil {
  26. fmt.Println(err)
  27. return
  28. }
  29. if err = f.SetSheetRow("Sheet1", addr, &row); err != nil {
  30. fmt.Println(err)
  31. return
  32. }
  33. }
  34. // 设置数据验证
  35. dvRange1 := excelize.NewDataValidation(true)
  36. dvRange1.Sqref = "D3:D3"
  37. dvRange1.SetSqrefDropList("$A$1:$B$1", true)
  38. if err = f.AddDataValidation("Sheet1", dvRange1); err != nil {
  39. fmt.Println(err)
  40. return
  41. }
  42. dvRange2 := excelize.NewDataValidation(true)
  43. dvRange2.Sqref = "E3:E3"
  44. dvRange2.SetSqrefDropList("INDIRECT(D3)", true)
  45. if err = f.AddDataValidation("Sheet1", dvRange2); err != nil {
  46. fmt.Println(err)
  47. return
  48. }
  49. // 设置自定义名称
  50. if err = f.SetDefinedName(&excelize.DefinedName{
  51. Name: "Fruits",
  52. RefersTo: "Sheet1!$A$2:$A$6",
  53. Scope: "Sheet1",
  54. }); err != nil {
  55. fmt.Println(err)
  56. return
  57. }
  58. if err = f.SetDefinedName(&excelize.DefinedName{
  59. Name: "Vegetables",
  60. RefersTo: "Sheet1!$B$2:$B$6",
  61. Scope: "Sheet1",
  62. }); err != nil {
  63. fmt.Println(err)
  64. return
  65. }
  66. // 自定义列宽
  67. for col, width := range map[string]float64{
  68. "A": 12, "B": 12, "C": 6, "D": 12, "E": 12} {
  69. if err = f.SetColWidth("Sheet1", col, col, width); err != nil {
  70. fmt.Println(err)
  71. return
  72. }
  73. }
  74. // 隐藏工作表网格线
  75. if err = f.SetSheetViewOptions("Sheet1", 0,
  76. excelize.ShowGridLines(false)); err != nil {
  77. fmt.Println(err)
  78. return
  79. }
  80. // 定义边框样式
  81. border := []excelize.Border{
  82. {Type: "top", Style: 1, Color: "cccccc"},
  83. {Type: "left", Style: 1, Color: "cccccc"},
  84. {Type: "right", Style: 1, Color: "cccccc"},
  85. {Type: "bottom", Style: 1, Color: "cccccc"},
  86. }
  87. // 定义单元格样式
  88. if cellsStyle, err = f.NewStyle(&excelize.Style{
  89. Font: &excelize.Font{Color: "333333"},
  90. Border: border}); err != nil {
  91. fmt.Println(err)
  92. return
  93. }
  94. // 定义标题行单元格样式
  95. if headerStyle, err = f.NewStyle(&excelize.Style{
  96. Font: &excelize.Font{Bold: true},
  97. Fill: excelize.Fill{
  98. Type: "pattern", Color: []string{"dae9f3"}, Pattern: 1},
  99. Border: border},
  100. ); err != nil {
  101. fmt.Println(err)
  102. return
  103. }
  104. // 为单元格设置样式
  105. if err = f.SetCellStyle("Sheet1", "A2", "B6", cellsStyle); err != nil {
  106. fmt.Println(err)
  107. return
  108. }
  109. if err = f.SetCellStyle("Sheet1", "D3", "E3", cellsStyle); err != nil {
  110. fmt.Println(err)
  111. return
  112. }
  113. // 为标题行设置样式
  114. if err = f.SetCellStyle("Sheet1", "A1", "B1", headerStyle); err != nil {
  115. fmt.Println(err)
  116. return
  117. }
  118. if err = f.SetCellStyle("Sheet1", "D2", "E2", headerStyle); err != nil {
  119. fmt.Println(err)
  120. return
  121. }
  122. // 保存工作簿
  123. if err := f.SaveAs("Book1.xlsx"); err != nil {
  124. fmt.Println(err)
  125. }
  126. }