feat:增加导出接口
This commit is contained in:
@ -4,8 +4,12 @@ import (
|
||||
"SciencesServer/config"
|
||||
"SciencesServer/serve/logger"
|
||||
"SciencesServer/utils"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/tealeg/xlsx"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"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 {
|
||||
return func(c *gin.Context) interface{} {
|
||||
resp := &response{
|
||||
|
Reference in New Issue
Block a user