Skip to content

Commit d278344

Browse files
author
奇淼(piexlmax
authored
Merge pull request #370 from hwb2017/develop-feature-excel
增加Excel导入导出示例功能
2 parents 21ef7bd + f60b0f1 commit d278344

File tree

15 files changed

+403
-143
lines changed

15 files changed

+403
-143
lines changed

server/api/v1/exa_file_upload_download.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"gin-vue-admin/model/request"
77
"gin-vue-admin/model/response"
88
"gin-vue-admin/service"
9+
"gin-vue-admin/utils"
910
"github.com/gin-gonic/gin"
1011
"go.uber.org/zap"
1112
)
@@ -78,3 +79,86 @@ func GetFileList(c *gin.Context) {
7879
},"获取成功", c)
7980
}
8081
}
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/config.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,8 @@ qiniu:
7676
use-https: false
7777
access-key: '25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ'
7878
secret-key: 'pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY'
79-
use-cdn-domains: false
79+
use-cdn-domains: false
80+
81+
# excel configuration
82+
excel:
83+
dir: './resource/excel/'

server/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ type Server struct {
1313
// oss
1414
Local Local `mapstructure:"local" json:"local" yaml:"local"`
1515
Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"`
16+
Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"`
1617
}

server/config/excel.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package config
2+
3+
type Excel struct {
4+
Dir string `mapstructure:"dir" json:"dir" yaml:"dir"`
5+
}

server/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module gin-vue-admin
33
go 1.14
44

55
require (
6+
github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.2
67
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
78
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
89
github.com/casbin/casbin v1.9.1
@@ -47,8 +48,7 @@ require (
4748
github.com/tebeka/strftime v0.1.3 // indirect
4849
github.com/unrolled/secure v1.0.7
4950
go.uber.org/zap v1.10.0
50-
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
51-
golang.org/x/sys v0.0.0-20200610111108-226ff32320da // indirect
51+
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
5252
golang.org/x/tools v0.0.0-20200324003944-a576cf524670 // indirect
5353
google.golang.org/protobuf v1.24.0 // indirect
5454
gopkg.in/ini.v1 v1.55.0 // indirect
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package request
2+
3+
import "gin-vue-admin/model"
4+
5+
type ExcelInfo struct {
6+
FileName string `json:"fileName"`
7+
InfoList []model.SysBaseMenu `json:"infoList"`
8+
}
6.49 KB
Binary file not shown.
8.79 KB
Binary file not shown.
8.7 KB
Binary file not shown.

server/router/exp_file_upload_and_download.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ func InitFileUploadAndDownloadRouter(Router *gin.RouterGroup) {
1515
FileUploadAndDownloadGroup.GET("/findFile", v1.FindFile) // 查询当前文件成功的切片
1616
FileUploadAndDownloadGroup.POST("/breakpointContinueFinish", v1.BreakpointContinueFinish) // 查询当前文件成功的切片
1717
FileUploadAndDownloadGroup.POST("/removeChunk", v1.RemoveChunk) // 查询当前文件成功的切片
18+
FileUploadAndDownloadGroup.POST("/importExcel", v1.ImportExcel) // 导入Excel
19+
FileUploadAndDownloadGroup.GET("/loadExcel", v1.LoadExcel) // 加载Excel数据
20+
FileUploadAndDownloadGroup.POST("/exportExcel", v1.ExportExcel) // 导出Excel
21+
FileUploadAndDownloadGroup.GET("/downloadTemplate", v1.DownloadTemplate) // 下载模板文件
1822
}
1923
}

0 commit comments

Comments
 (0)