exa_excel.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package example
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "go.uber.org/zap"
  5. "log-server/global"
  6. "log-server/model/common/response"
  7. "log-server/model/example"
  8. "os"
  9. "strings"
  10. )
  11. type ExcelApi struct{}
  12. // /excel/importExcel 接口,与upload接口作用类似,只是把文件存到resource/excel目录下,用于导入Excel时存放Excel文件(ExcelImport.xlsx)
  13. // /excel/loadExcel接口,用于读取resource/excel目录下的文件((ExcelImport.xlsx)并加载为[]model.SysBaseMenu类型的示例数据
  14. // /excel/exportExcel 接口,用于读取前端传来的tableData,生成Excel文件并返回
  15. // /excel/downloadTemplate 接口,用于下载resource/excel目录下的 ExcelTemplate.xlsx 文件,作为导入的模板
  16. // @Tags excel
  17. // @Summary 导出Excel
  18. // @Security ApiKeyAuth
  19. // @accept application/json
  20. // @Produce application/octet-stream
  21. // @Param data body example.ExcelInfo true "导出Excel文件信息"
  22. // @Success 200
  23. // @Router /excel/exportExcel [post]
  24. func (e *ExcelApi) ExportExcel(c *gin.Context) {
  25. var excelInfo example.ExcelInfo
  26. _ = c.ShouldBindJSON(&excelInfo)
  27. if strings.Index(excelInfo.FileName, "..") > -1 {
  28. response.FailWithMessage("包含非法字符", c)
  29. return
  30. }
  31. filePath := global.GVA_CONFIG.Excel.Dir + excelInfo.FileName
  32. err := excelService.ParseInfoList2Excel(excelInfo.InfoList, filePath)
  33. if err != nil {
  34. global.GVA_LOG.Error("转换Excel失败!", zap.Error(err))
  35. response.FailWithMessage("转换Excel失败", c)
  36. return
  37. }
  38. c.Writer.Header().Add("success", "true")
  39. c.File(filePath)
  40. }
  41. // @Tags excel
  42. // @Summary 导入Excel文件
  43. // @Security ApiKeyAuth
  44. // @accept multipart/form-data
  45. // @Produce application/json
  46. // @Param file formData file true "导入Excel文件"
  47. // @Success 200 {object} response.Response{msg=string} "导入Excel文件"
  48. // @Router /excel/importExcel [post]
  49. func (e *ExcelApi) ImportExcel(c *gin.Context) {
  50. _, header, err := c.Request.FormFile("file")
  51. if err != nil {
  52. global.GVA_LOG.Error("接收文件失败!", zap.Error(err))
  53. response.FailWithMessage("接收文件失败", c)
  54. return
  55. }
  56. _ = c.SaveUploadedFile(header, global.GVA_CONFIG.Excel.Dir+"ExcelImport.xlsx")
  57. response.OkWithMessage("导入成功", c)
  58. }
  59. // @Tags excel
  60. // @Summary 加载Excel数据
  61. // @Security ApiKeyAuth
  62. // @Produce application/json
  63. // @Success 200 {object} response.Response{data=response.PageResult,msg=string} "加载Excel数据,返回包括列表,总数,页码,每页数量"
  64. // @Router /excel/loadExcel [get]
  65. func (e *ExcelApi) LoadExcel(c *gin.Context) {
  66. menus, err := excelService.ParseExcel2InfoList()
  67. if err != nil {
  68. global.GVA_LOG.Error("加载数据失败!", zap.Error(err))
  69. response.FailWithMessage("加载数据失败", c)
  70. return
  71. }
  72. response.OkWithDetailed(response.PageResult{
  73. List: menus,
  74. Total: int64(len(menus)),
  75. Page: 1,
  76. PageSize: 999,
  77. }, "加载数据成功", c)
  78. }
  79. // @Tags excel
  80. // @Summary 下载模板
  81. // @Security ApiKeyAuth
  82. // @accept multipart/form-data
  83. // @Produce application/json
  84. // @Param fileName query string true "模板名称"
  85. // @Success 200
  86. // @Router /excel/downloadTemplate [get]
  87. func (e *ExcelApi) DownloadTemplate(c *gin.Context) {
  88. fileName := c.Query("fileName")
  89. filePath := global.GVA_CONFIG.Excel.Dir + fileName
  90. fi, err := os.Stat(filePath)
  91. if err != nil {
  92. global.GVA_LOG.Error("文件不存在!", zap.Error(err))
  93. response.FailWithMessage("文件不存在", c)
  94. return
  95. }
  96. if fi.IsDir() {
  97. global.GVA_LOG.Error("不支持下载文件夹!", zap.Error(err))
  98. response.FailWithMessage("不支持下载文件夹", c)
  99. return
  100. }
  101. c.Writer.Header().Add("success", "true")
  102. c.File(filePath)
  103. }