feat:增加导出接口
This commit is contained in:
@ -4,8 +4,12 @@ import (
|
|||||||
"SciencesServer/config"
|
"SciencesServer/config"
|
||||||
"SciencesServer/serve/logger"
|
"SciencesServer/serve/logger"
|
||||||
"SciencesServer/utils"
|
"SciencesServer/utils"
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"github.com/tealeg/xlsx"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gin-gonic/gin/binding"
|
"github.com/gin-gonic/gin/binding"
|
||||||
@ -61,6 +65,21 @@ func Bind(req interface{}) ApiHandle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// APIDownloadExcel 下载Excel文件
|
||||||
|
func APIDownloadExcel(fileName string, file *xlsx.File) ApiHandle {
|
||||||
|
return func(c *gin.Context) interface{} {
|
||||||
|
c.Writer.Header().Set("Content-Type", "application/octet-stream")
|
||||||
|
disposition := fmt.Sprintf("attachment; filename=\"%s-%s.xlsx\"", fileName,
|
||||||
|
time.Now().Format("2006-01-02"))
|
||||||
|
c.Writer.Header().Set("Content-Disposition", disposition)
|
||||||
|
buffer := new(bytes.Buffer)
|
||||||
|
_ = file.Write(buffer)
|
||||||
|
content := bytes.NewReader(buffer.Bytes())
|
||||||
|
http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func APISuccess(data ...interface{}) ApiHandle {
|
func APISuccess(data ...interface{}) ApiHandle {
|
||||||
return func(c *gin.Context) interface{} {
|
return func(c *gin.Context) interface{} {
|
||||||
resp := &response{
|
resp := &response{
|
||||||
|
Reference in New Issue
Block a user