Skip to content

Commit 171be65

Browse files
author
pixel
committed
excel导入导出功能合并并且细节修改完成
1 parent 0f6613c commit 171be65

File tree

14 files changed

+218
-183
lines changed

14 files changed

+218
-183
lines changed

server/api/v1/exa_excel.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package v1
2+
3+
import (
4+
"gin-vue-admin/global"
5+
"gin-vue-admin/model"
6+
"gin-vue-admin/model/response"
7+
"gin-vue-admin/service"
8+
"gin-vue-admin/utils"
9+
"github.com/gin-gonic/gin"
10+
"go.uber.org/zap"
11+
)
12+
13+
// /excel/importExcel 接口,与upload接口作用类似,只是把文件存到resource/excel目录下,用于导入Excel时存放Excel文件(ExcelImport.xlsx)
14+
// /excel/loadExcel接口,用于读取resource/excel目录下的文件((ExcelImport.xlsx)并加载为[]model.SysBaseMenu类型的示例数据
15+
// /excel/exportExcel 接口,用于读取前端传来的tableData,生成Excel文件并返回
16+
// /excel/downloadTemplate 接口,用于下载resource/excel目录下的 ExcelTemplate.xlsx 文件,作为导入的模板
17+
18+
// @Tags excel
19+
// @Summary 导出Excel
20+
// @Security ApiKeyAuth
21+
// @accept application/json
22+
// @Produce application/octet-stream
23+
// @Param data body model.ExcelInfo true "导出Excel文件信息"
24+
// @Success 200
25+
// @Router /excel/exportExcel [post]
26+
func ExportExcel(c *gin.Context) {
27+
var excelInfo model.ExcelInfo
28+
_ = c.ShouldBindJSON(&excelInfo)
29+
filePath := global.GVA_CONFIG.Excel.Dir + excelInfo.FileName
30+
err := service.ParseInfoList2Excel(excelInfo.InfoList, filePath)
31+
if err != nil {
32+
global.GVA_LOG.Error("转换Excel失败!", zap.Any("err", err))
33+
response.FailWithMessage("转换Excel失败", c)
34+
return
35+
}
36+
c.Writer.Header().Add("success", "true")
37+
c.File(filePath)
38+
}
39+
40+
// @Tags excel
41+
// @Summary 导入Excel文件
42+
// @Security ApiKeyAuth
43+
// @accept multipart/form-data
44+
// @Produce application/json
45+
// @Param file formData file true "导入Excel文件"
46+
// @Success 200 {string} string "{"success":true,"data":{},"msg":"导入成功"}"
47+
// @Router /excel/importExcel [post]
48+
func ImportExcel(c *gin.Context) {
49+
_, header, err := c.Request.FormFile("file")
50+
if err != nil {
51+
global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
52+
response.FailWithMessage("接收文件失败", c)
53+
return
54+
}
55+
_ = c.SaveUploadedFile(header, global.GVA_CONFIG.Excel.Dir+"ExcelImport.xlsx")
56+
response.OkWithMessage("导入成功", c)
57+
}
58+
59+
// @Tags excel
60+
// @Summary 加载Excel数据
61+
// @Security ApiKeyAuth
62+
// @Produce application/json
63+
// @Success 200 {string} string "{"success":true,"data":{},"msg":"加载数据成功"}"
64+
// @Router /excel/loadExcel [get]
65+
func LoadExcel(c *gin.Context) {
66+
menus, err := service.ParseExcel2InfoList()
67+
if err != nil {
68+
global.GVA_LOG.Error("加载数据失败", zap.Any("err", 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+
80+
// @Tags excel
81+
// @Summary 下载模板
82+
// @Security ApiKeyAuth
83+
// @accept multipart/form-data
84+
// @Produce application/json
85+
// @Param fileName query fileName true "模板名称"
86+
// @Success 200
87+
// @Router /excel/downloadTemplate [get]
88+
func DownloadTemplate(c *gin.Context) {
89+
fileName := c.Query("fileName")
90+
filePath := global.GVA_CONFIG.Excel.Dir + fileName
91+
ok, err := utils.PathExists(filePath)
92+
if !ok || err != nil {
93+
global.GVA_LOG.Error("文件不存在", zap.Any("err", err))
94+
response.FailWithMessage("文件不存在", c)
95+
return
96+
}
97+
c.Writer.Header().Add("success", "true")
98+
c.File(filePath)
99+
}

server/api/v1/exa_file_upload_download.go

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"gin-vue-admin/model/request"
77
"gin-vue-admin/model/response"
88
"gin-vue-admin/service"
9-
"gin-vue-admin/utils"
109
"github.com/gin-gonic/gin"
1110
"go.uber.org/zap"
1211
)
@@ -76,89 +75,6 @@ func GetFileList(c *gin.Context) {
7675
Total: total,
7776
Page: pageInfo.Page,
7877
PageSize: pageInfo.PageSize,
79-
},"获取成功", c)
78+
}, "获取成功", c)
8079
}
8180
}
82-
83-
// @Tags ExaFileUploadAndDownload
84-
// @Summary 导出Excel
85-
// @Security ApiKeyAuth
86-
// @accept application/json
87-
// @Produce application/octet-stream
88-
// @Param data body request.ExcelInfo true "导出Excel文件信息"
89-
// @Success 200
90-
// @Router /fileUploadAndDownload/exportExcel [post]
91-
func ExportExcel(c *gin.Context) {
92-
var excelInfo request.ExcelInfo
93-
c.ShouldBindJSON(&excelInfo)
94-
filePath := global.GVA_CONFIG.Excel.Dir+excelInfo.FileName
95-
err := service.ParseInfoList2Excel(excelInfo.InfoList, filePath)
96-
if err != nil {
97-
global.GVA_LOG.Error("转换Excel失败!", zap.Any("err", err))
98-
response.FailWithMessage("转换Excel失败", c)
99-
return
100-
}
101-
c.Writer.Header().Add("success", "true")
102-
c.File(filePath)
103-
}
104-
105-
// @Tags ExaFileUploadAndDownload
106-
// @Summary 导入Excel文件
107-
// @Security ApiKeyAuth
108-
// @accept multipart/form-data
109-
// @Produce application/json
110-
// @Param file formData file true "导入Excel文件"
111-
// @Success 200 {string} string "{"success":true,"data":{},"msg":"导入成功"}"
112-
// @Router /fileUploadAndDownload/importExcel [post]
113-
func ImportExcel(c *gin.Context) {
114-
_, header, err := c.Request.FormFile("file")
115-
if err != nil {
116-
global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
117-
response.FailWithMessage("接收文件失败", c)
118-
return
119-
}
120-
c.SaveUploadedFile(header, global.GVA_CONFIG.Excel.Dir+"ExcelImport.xlsx")
121-
response.OkWithMessage("导入成功", c)
122-
}
123-
124-
// @Tags ExaFileUploadAndDownload
125-
// @Summary 加载Excel数据
126-
// @Security ApiKeyAuth
127-
// @Produce application/json
128-
// @Success 200 {string} string "{"success":true,"data":{},"msg":"加载数据成功"}"
129-
// @Router /fileUploadAndDownload/loadExcel [get]
130-
func LoadExcel(c *gin.Context) {
131-
menus, err := service.ParseExcel2InfoList()
132-
if err != nil {
133-
global.GVA_LOG.Error("加载数据失败", zap.Any("err", err))
134-
response.FailWithMessage("加载数据失败", c)
135-
return
136-
}
137-
response.OkWithDetailed(response.PageResult{
138-
List: menus,
139-
Total: int64(len(menus)),
140-
Page: 1,
141-
PageSize: 999,
142-
},"加载数据成功", c)
143-
}
144-
145-
// @Tags ExaFileUploadAndDownload
146-
// @Summary 下载模板
147-
// @Security ApiKeyAuth
148-
// @accept multipart/form-data
149-
// @Produce application/json
150-
// @Param fileName query fileName true "模板名称"
151-
// @Success 200
152-
// @Router /fileUploadAndDownload/downloadTemplate [get]
153-
func DownloadTemplate(c *gin.Context) {
154-
fileName := c.Query("fileName")
155-
filePath := global.GVA_CONFIG.Excel.Dir+fileName
156-
ok, err := utils.PathExists(filePath)
157-
if !ok || err != nil {
158-
global.GVA_LOG.Error("文件不存在", zap.Any("err", err))
159-
response.FailWithMessage("文件不存在", c)
160-
return
161-
}
162-
c.Writer.Header().Add("success", "true")
163-
c.File(filePath)
164-
}

server/cmd/information/system/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ var apis = []model.SysApi{
9393
{global.GVA_MODEL{ID: 78, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "/workflowProcess/getWorkflowMoveByID", "根据id获取当前节点详情和历史", "workflowProcess", "GET"},
9494
{global.GVA_MODEL{ID: 79, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "/workflowProcess/completeWorkflowMove", "提交工作流", "workflowProcess", "POST"},
9595
{global.GVA_MODEL{ID: 80, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "/autoCode/preview", "预览自动化代码", "autoCode", "POST"},
96+
{global.GVA_MODEL{ID: 81, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "/excel/importExcel", "预览自动化代码", "autoCode", "POST"},
97+
{global.GVA_MODEL{ID: 82, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "/excel/loadExcel", "预览自动化代码", "autoCode", "POST"},
98+
{global.GVA_MODEL{ID: 83, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "/excel/exportExcel", "预览自动化代码", "autoCode", "POST"},
99+
{global.GVA_MODEL{ID: 84, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "/excel/downloadTemplate", "预览自动化代码", "autoCode", "POST"},
96100
}
97101

98102
//@author: [SliverHorn](https://github.com/SliverHorn)

server/cmd/information/system/casbin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ var carbines = []gormadapter.CasbinRule{
9292
{PType: "p", V0: "888", V1: "/workflowProcess/getMyStated", V2: "GET"},
9393
{PType: "p", V0: "888", V1: "/workflowProcess/getMyNeed", V2: "GET"},
9494
{PType: "p", V0: "888", V1: "/workflowProcess/getWorkflowMoveByID", V2: "GET"},
95+
{PType: "p", V0: "888", V1: "/excel/importExcel", V2: "POST"},
96+
{PType: "p", V0: "888", V1: "/excel/loadExcel", V2: "GET"},
97+
{PType: "p", V0: "888", V1: "/excel/exportExcel", V2: "POST"},
98+
{PType: "p", V0: "888", V1: "/excel/downloadTemplate", V2: "GET"},
9599
{PType: "p", V0: "8881", V1: "/base/login", V2: "POST"},
96100
{PType: "p", V0: "8881", V1: "/user/register", V2: "POST"},
97101
{PType: "p", V0: "8881", V1: "/api/createApi", V2: "POST"},

server/initialize/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func Routers() *gin.Engine {
4747
router.InitSysDictionaryDetailRouter(PrivateGroup) // 字典详情管理
4848
router.InitFileUploadAndDownloadRouter(PrivateGroup) // 文件上传下载功能路由
4949
router.InitWorkflowProcessRouter(PrivateGroup) // 工作流相关接口
50+
router.InitExcelRouter(PrivateGroup) // 表格导入导出
5051
}
5152
global.GVA_LOG.Info("router register success")
5253
return Router

server/model/exa_excel.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package model
2+
3+
type ExcelInfo struct {
4+
FileName string `json:"fileName"`
5+
InfoList []SysBaseMenu `json:"infoList"`
6+
}

server/model/request/exa_file_upload_and_download.go

Lines changed: 0 additions & 8 deletions
This file was deleted.
2.2 KB
Binary file not shown.
File renamed without changes.

server/router/exa_excel.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package router
2+
3+
import (
4+
"gin-vue-admin/api/v1"
5+
"github.com/gin-gonic/gin"
6+
)
7+
8+
func InitExcelRouter(Router *gin.RouterGroup) {
9+
FileUploadAndDownloadGroup := Router.Group("excel")
10+
{
11+
FileUploadAndDownloadGroup.POST("/importExcel", v1.ImportExcel) // 导入Excel
12+
FileUploadAndDownloadGroup.GET("/loadExcel", v1.LoadExcel) // 加载Excel数据
13+
FileUploadAndDownloadGroup.POST("/exportExcel", v1.ExportExcel) // 导出Excel
14+
FileUploadAndDownloadGroup.GET("/downloadTemplate", v1.DownloadTemplate) // 下载模板文件
15+
}
16+
}

0 commit comments

Comments
 (0)