mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-12-24 08:12:55 +08:00
修复导入获取数据下标不对
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"x_admin/config"
|
||||
"x_admin/core/request"
|
||||
@@ -10,6 +14,7 @@ import (
|
||||
"x_admin/util"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
// func AdminRoute(rg *gin.RouterGroup) {
|
||||
@@ -67,6 +72,46 @@ func (ah AdminHandler) ExportFile(c *gin.Context) {
|
||||
// f.Write(c.Writer)
|
||||
}
|
||||
|
||||
// 导入文件
|
||||
func (ah AdminHandler) ImportFile(c *gin.Context) {
|
||||
// file, err := c.FormFile("file")
|
||||
// if err != nil {
|
||||
// c.String(500, "上传文件出错")
|
||||
// }
|
||||
// fmt.Println(file)
|
||||
|
||||
// 单文件
|
||||
file, _, err := c.Request.FormFile("file")
|
||||
if err != nil {
|
||||
response.FailWithMsg(c, response.SystemError, "文件不存在")
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
// 创建缓冲区
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
// 将文件内容复制到缓冲区
|
||||
_, err = io.Copy(buf, file)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Failed to read file")
|
||||
return
|
||||
}
|
||||
// 创建Excel文件对象
|
||||
f, err := excelize.OpenReader(bytes.NewReader(buf.Bytes()))
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Failed to open Excel file")
|
||||
return
|
||||
}
|
||||
importList := []SystemAuthAdminResp{}
|
||||
err = excel.ImportExcel(f, &importList, 1, 2)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
for _, t := range importList {
|
||||
fmt.Printf("%#v", t)
|
||||
}
|
||||
}
|
||||
|
||||
// list 管理员列表
|
||||
func (ah AdminHandler) List(c *gin.Context) {
|
||||
var page request.PageReq
|
||||
|
||||
@@ -32,6 +32,8 @@ func AdminRoute(rg *gin.RouterGroup) {
|
||||
|
||||
rg.GET("/admin/ExportFile", middleware.RecordLog("管理员导出"), handle.ExportFile)
|
||||
|
||||
rg.POST("/admin/ImportFile", handle.ImportFile)
|
||||
|
||||
}
|
||||
func RoleRoute(rg *gin.RouterGroup) {
|
||||
// db := core.GetDB()
|
||||
|
||||
Reference in New Issue
Block a user