Skip to content

Commit 01a5b85

Browse files
author
汪先森
committed
global/response/response.go: add download xlsx method
1 parent bafe18f commit 01a5b85

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

server/global/response/response.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package response
22

33
import (
4+
"github.com/360EntSecGroup-Skylar/excelize"
45
"github.com/gin-gonic/gin"
56
"net/http"
7+
"strconv"
68
)
79

810
type Response struct {
@@ -41,6 +43,20 @@ func OkDetailed(data interface{}, message string, c *gin.Context) {
4143
Result(SUCCESS, data, message, c)
4244
}
4345

46+
func OkWithXlsx(list []interface{}, title []string, fileName string, c *gin.Context) {
47+
file := excelize.NewFile()
48+
file.SetSheetRow("Sheet1", "A1", &title)
49+
for i, v := range list {
50+
// 第一行被title占用
51+
lint := strconv.Itoa(i + 2)
52+
file.SetSheetRow("Sheet1", "A"+lint, v)
53+
}
54+
c.Header("Content-Type", "application/octet-stream")
55+
c.Header("Content-Disposition", "attachment; filename="+fileName)
56+
c.Header("Content-Transfer-Encoding", "binary")
57+
_ = file.Write(c.Writer)
58+
}
59+
4460
func Fail(c *gin.Context) {
4561
Result(ERROR, map[string]interface{}{}, "操作失败", c)
4662
}

server/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module gin-vue-admin
33
go 1.12
44

55
require (
6+
github.com/360EntSecGroup-Skylar/excelize v1.4.1 // indirect
67
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
78
github.com/casbin/casbin v1.9.1
89
github.com/casbin/casbin/v2 v2.11.0

0 commit comments

Comments
 (0)