interface.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package excel
  2. import (
  3. "fmt"
  4. "kpt-tmr-group/config"
  5. "kpt-tmr-group/model"
  6. "kpt-tmr-group/pkg/di"
  7. "math/rand"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "github.com/xuri/excelize/v2"
  11. )
  12. type SheetService interface {
  13. ExportToPath(params []map[string]string, data []map[string]interface{}, path string) (string, error)
  14. ExportToWeb(params []map[string]string, data []map[string]interface{}, c *gin.Context)
  15. writeTop(params []map[string]string)
  16. writeData(params []map[string]string, data []map[string]interface{})
  17. Export(params []map[string]string, data []map[string]interface{})
  18. }
  19. var Module = di.Provide(NewMyExcel)
  20. func NewMyExcel(cfg *config.AppConfig) *ExcelServer {
  21. return &ExcelServer{File: createFile(cfg), SheetName: cfg.ExcelSetting.SheetName}
  22. }
  23. func createFile(cfg *config.AppConfig) *excelize.File {
  24. f := excelize.NewFile()
  25. // 创建一个默认工作表
  26. index, _ := f.NewSheet(cfg.ExcelSetting.SheetName)
  27. // 设置工作簿的默认工作表
  28. f.SetActiveSheet(index)
  29. return f
  30. }
  31. func createFileName() string {
  32. name := time.Now().Format(model.LayoutTime)
  33. rand.Seed(time.Now().UnixNano())
  34. return fmt.Sprintf("excle-%v-%v.xlsx", name, rand.Int63n(time.Now().Unix()))
  35. }
  36. /*import (
  37. "go-excel/app/excelize"
  38. "go-excel/app/model"
  39. config "go-excel/common"
  40. "github.com/gin-gonic/gin"
  41. )
  42. //获取所有用户数据-excel
  43. func GetAllUserExportToWeb(ctx *gin.Context) {
  44. var users []model.TUser
  45. db := config.GetDB()
  46. db.Find(&users)
  47. //定义首行标题
  48. dataKey := make([]map[string]string, 0)
  49. dataKey = append(dataKey, map[string]string{
  50. "key": "id",
  51. "title": "索引",
  52. "width": "20",
  53. "is_num": "0",
  54. })
  55. dataKey = append(dataKey, map[string]string{
  56. "key": "username",
  57. "title": "用户名",
  58. "width": "20",
  59. "is_num": "0",
  60. })
  61. dataKey = append(dataKey, map[string]string{
  62. "key": "remark",
  63. "title": "备注",
  64. "width": "20",
  65. "is_num": "0",
  66. })
  67. //填充数据
  68. data := make([]map[string]interface{}, 0)
  69. if len(users) > 0 {
  70. for _, v := range users {
  71. data = append(data, map[string]interface{}{
  72. "id": v.ID,
  73. "username": v.Username,
  74. "remark": v.Remark,
  75. })
  76. }
  77. }
  78. ex := excelize.NewMyExcel()
  79. // ex.ExportToWeb(dataKey, data, ctx)
  80. //保存到D盘
  81. ex.ExportToPath(dataKey, data, "D:/")
  82. }
  83. //excel 导出
  84. func GetUserExcelByMap(ctx *gin.Context) {
  85. var users []model.TUser
  86. db := config.GetDB()
  87. db.Find(&users)
  88. titles := []string{"ID", "用户名", "备注"}
  89. ex := excelize.NewMyExcel()
  90. var datas []interface{}
  91. for _, v := range users {
  92. //这里最好新建一个struct 和titles一致,不然users里面的多余的字段也会写进去
  93. datas = append(datas, model.TUser{
  94. ID: v.ID,
  95. Username: v.Username,
  96. Remark: v.Remark,
  97. })
  98. }
  99. ex.ExportExcelByStruct(titles, datas, "用户数据", "用户", ctx)
  100. }*/