exa_excel_parse.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package example
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
  6. "strconv"
  7. "github.com/flipped-aurora/gin-vue-admin/server/global"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  9. "github.com/xuri/excelize/v2"
  10. )
  11. type ExcelService struct {
  12. }
  13. func (exa *ExcelService) ParseInfoList2Excel(infoList []system.SysBaseMenu, filePath string) error {
  14. excel := excelize.NewFile()
  15. excel.SetSheetRow("Sheet1", "A1", &[]string{"ID", "路由Name", "路由Path", "是否隐藏", "父节点", "排序", "文件名称"})
  16. for i, menu := range infoList {
  17. axis := fmt.Sprintf("A%d", i+2)
  18. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  19. menu.ID,
  20. menu.Name,
  21. menu.Path,
  22. menu.Hidden,
  23. menu.ParentId,
  24. menu.Sort,
  25. menu.Component,
  26. })
  27. }
  28. err := excel.SaveAs(filePath)
  29. return err
  30. }
  31. func (exa *ExcelService) ProblemInfoList2Excel(infoList []autocode.ProblemInfo, filePath string) error {
  32. excel := excelize.NewFile()
  33. excel.SetSheetRow("Sheet1", "A1", &[]string{"ID", "问题发布时间", "问题处理时间", "问题视频地址", "问题图片", "站点名称", "站点位置", "问题ID", "站点类型", "责任部门", "处理状态", "处理图片", "处理内容", "处理人"})
  34. for i, problemInfo := range infoList {
  35. axis := fmt.Sprintf("A%d", i+2)
  36. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  37. problemInfo.ID,
  38. problemInfo.CreatedAt,
  39. problemInfo.UpdatedAt,
  40. problemInfo.Video,
  41. problemInfo.Imgs,
  42. problemInfo.SiteName,
  43. "https://apis.map.qq.com/uri/v1/marker?marker=coord:" + problemInfo.Position + ";title:" + problemInfo.SiteName + ";addr:" + problemInfo.SiteType,
  44. problemInfo.Matter,
  45. problemInfo.SiteType,
  46. problemInfo.Department,
  47. problemInfo.Status,
  48. problemInfo.HandImgs,
  49. problemInfo.HandText,
  50. problemInfo.Handler,
  51. })
  52. }
  53. err := excel.SaveAs(filePath)
  54. return err
  55. }
  56. func (exa *ExcelService) ParseExcel2InfoList() ([]system.SysBaseMenu, error) {
  57. skipHeader := true
  58. fixedHeader := []string{"ID", "路由Name", "路由Path", "是否隐藏", "父节点", "排序", "文件名称"}
  59. file, err := excelize.OpenFile(global.GVA_CONFIG.Excel.Dir + "ExcelImport.xlsx")
  60. if err != nil {
  61. return nil, err
  62. }
  63. menus := make([]system.SysBaseMenu, 0)
  64. rows, err := file.Rows("Sheet1")
  65. if err != nil {
  66. return nil, err
  67. }
  68. for rows.Next() {
  69. row, err := rows.Columns()
  70. if err != nil {
  71. return nil, err
  72. }
  73. if skipHeader {
  74. if exa.compareStrSlice(row, fixedHeader) {
  75. skipHeader = false
  76. continue
  77. } else {
  78. return nil, errors.New("Excel格式错误")
  79. }
  80. }
  81. if len(row) != len(fixedHeader) {
  82. continue
  83. }
  84. id, _ := strconv.Atoi(row[0])
  85. hidden, _ := strconv.ParseBool(row[3])
  86. sort, _ := strconv.Atoi(row[5])
  87. menu := system.SysBaseMenu{
  88. GVA_MODEL: global.GVA_MODEL{
  89. ID: uint(id),
  90. },
  91. Name: row[1],
  92. Path: row[2],
  93. Hidden: hidden,
  94. ParentId: row[4],
  95. Sort: sort,
  96. Component: row[6],
  97. }
  98. menus = append(menus, menu)
  99. }
  100. return menus, nil
  101. }
  102. func (exa *ExcelService) compareStrSlice(a, b []string) bool {
  103. if len(a) != len(b) {
  104. return false
  105. }
  106. if (b == nil) != (a == nil) {
  107. return false
  108. }
  109. for key, value := range a {
  110. if value != b[key] {
  111. return false
  112. }
  113. }
  114. return true
  115. }