修复导入获取数据下标不对

This commit is contained in:
xiangheng
2024-02-26 12:39:33 +08:00
parent df43e4e7dd
commit c27d7d4e1e
8 changed files with 129 additions and 25 deletions

View File

@@ -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

View File

@@ -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()