You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.0 KiB

  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "gin-vue-admin/global"
  6. "gin-vue-admin/model"
  7. "github.com/360EntSecGroup-Skylar/excelize/v2"
  8. "strconv"
  9. )
  10. func ParseInfoList2Excel(infoList []model.SysBaseMenu, filePath string) error {
  11. excel := excelize.NewFile()
  12. excel.SetSheetRow("Sheet1","A1",&[]string{"ID","路由Name","路由Path","是否隐藏","父节点","排序","文件名称"})
  13. for i, menu := range infoList {
  14. axis := fmt.Sprintf("A%d",i+2)
  15. excel.SetSheetRow("Sheet1",axis,&[]interface{}{
  16. menu.ID,
  17. menu.Name,
  18. menu.Path,
  19. menu.Hidden,
  20. menu.ParentId,
  21. menu.Sort,
  22. menu.Component,
  23. })
  24. }
  25. excel.SaveAs(filePath)
  26. return nil
  27. }
  28. func ParseExcel2InfoList() ([]model.SysBaseMenu, error) {
  29. skipHeader := true
  30. fixedHeader := []string{"ID","路由Name","路由Path","是否隐藏","父节点","排序","文件名称"}
  31. file, err := excelize.OpenFile(global.GVA_CONFIG.Excel.Dir+"ExcelImport.xlsx")
  32. if err != nil {
  33. return nil, err
  34. }
  35. menus := make([]model.SysBaseMenu, 0)
  36. rows, err := file.Rows("Sheet1")
  37. if err != nil {
  38. return nil, err
  39. }
  40. for rows.Next() {
  41. row, err := rows.Columns()
  42. if err != nil {
  43. return nil, err
  44. }
  45. if skipHeader {
  46. if compareStrSlice(row, fixedHeader) {
  47. skipHeader = false
  48. continue
  49. } else {
  50. return nil, errors.New("Excel格式错误")
  51. }
  52. }
  53. if len(row) != len(fixedHeader) {
  54. continue
  55. }
  56. id, _ := strconv.Atoi(row[0])
  57. hidden, _ := strconv.ParseBool(row[3])
  58. sort, _ := strconv.Atoi(row[5])
  59. menu := model.SysBaseMenu{
  60. GVA_MODEL: global.GVA_MODEL{
  61. ID: uint(id),
  62. },
  63. Name: row[1],
  64. Path: row[2],
  65. Hidden: hidden,
  66. ParentId: row[4],
  67. Sort: sort,
  68. Component: row[6],
  69. }
  70. menus = append(menus, menu)
  71. }
  72. return menus, nil
  73. }
  74. func compareStrSlice(a, b []string) bool {
  75. if len(a) != len(b) {
  76. return false
  77. }
  78. if (b == nil) != (a == nil) {
  79. return false
  80. }
  81. for key, value := range a {
  82. if value != b[key] {
  83. return false
  84. }
  85. }
  86. return true
  87. }