级联列表

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/xuri/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")
  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)")
  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. disable := false
  76. if err := f.SetSheetView("Sheet1", -1, &excelize.ViewOptions{
  77. ShowGridLines: &disable,
  78. }); err != nil {
  79. fmt.Println(err)
  80. }
  81. // 定义边框样式
  82. border := []excelize.Border{
  83. {Type: "top", Style: 1, Color: "cccccc"},
  84. {Type: "left", Style: 1, Color: "cccccc"},
  85. {Type: "right", Style: 1, Color: "cccccc"},
  86. {Type: "bottom", Style: 1, Color: "cccccc"},
  87. }
  88. // 定义单元格样式
  89. if cellsStyle, err = f.NewStyle(&excelize.Style{
  90. Font: &excelize.Font{Color: "333333"},
  91. Border: border}); err != nil {
  92. fmt.Println(err)
  93. return
  94. }
  95. // 定义标题行单元格样式
  96. if headerStyle, err = f.NewStyle(&excelize.Style{
  97. Font: &excelize.Font{Bold: true},
  98. Fill: excelize.Fill{
  99. Type: "pattern", Color: []string{"dae9f3"}, Pattern: 1},
  100. Border: border},
  101. ); err != nil {
  102. fmt.Println(err)
  103. return
  104. }
  105. // 为单元格设置样式
  106. if err = f.SetCellStyle("Sheet1", "A2", "B6", cellsStyle); err != nil {
  107. fmt.Println(err)
  108. return
  109. }
  110. if err = f.SetCellStyle("Sheet1", "D3", "E3", cellsStyle); err != nil {
  111. fmt.Println(err)
  112. return
  113. }
  114. // 为标题行设置样式
  115. if err = f.SetCellStyle("Sheet1", "A1", "B1", headerStyle); err != nil {
  116. fmt.Println(err)
  117. return
  118. }
  119. if err = f.SetCellStyle("Sheet1", "D2", "E2", headerStyle); err != nil {
  120. fmt.Println(err)
  121. return
  122. }
  123. // 保存工作簿
  124. if err := f.SaveAs("Book1.xlsx"); err != nil {
  125. fmt.Println(err)
  126. }
  127. }