• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

gogin读取excel插入Mysql

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
代码如下:

gin 方法 func ImportDiamonds(c
*gin.Context) { var data models.Diamonds form, _ := c.MultipartForm() files := form.File["upload"] guid := uuid.New().String() filePath := "static/uploadfile/" + guid + ".xlsx" for _, file := range files { fmt.Println(file.Filename) _ = c.SaveUploadedFile(file, filePath) } if filePath == "" { c.JSON(400,gin.H{ "msg":"没有这样的文件", "code":400, }) } data.CreateBy = tools.GetUserIdStr(c) err := data.ImportPro(filePath) tools.HasError(err, "import fail", 500) app.OK(c, nil, "import success") } // 读取方法 func (e *Diamonds) ImportPro(filePath string) error { xlFile, err := xlsx.OpenFile(filePath) if err != nil { fmt.Println(err) } //获取行数 // length := len(xlFile.Sheets[0].Rows) //开辟除表头外的行数的数组内存 // resourceArr := make([]string, length-1) for _, sheet := range xlFile.Sheets { //遍历每一行 for rowIndex, row := range sheet.Rows { //跳过第一行表头信息 if rowIndex == 0 { // for _, cell := range row.Cells { // text := cell.String() // fmt.Printf("%s\n", text) // } continue } //遍历每一个单元 msg := Msg{} msg.Price = row.Cells[0].Value msg.Weight = row.Cells[1].Value msg.Color = row.Cells[2].Value msg.Neatness = row.Cells[3].Value msg.Cut = row.Cells[4].Value msg.Symmetric = row.Cells[5].Value msg.Polishing = row.Cells[6].Value msg.Fluorescence = row.Cells[7].Value msg.Shape = row.Cells[8].Value msg.Certificate = row.Cells[9].Value msg.Location = row.Cells[10].Value msg.Classify = 1 e.AddDiamonds(msg.Color,msg.Neatness,msg.Cut,msg.Symmetric,msg.Polishing,msg.Fluorescence,msg.Shape,msg.Certificate,msg.Location,msg.Classify) fmt.Println("msg:------ ",msg) \ } } return nil // fmt.Println(resourceArr) // return resourceArr } // 入库gorm func (e *Diamonds) AddDiamonds( color string,neatness string, cut string, symmetric string ,polishing string, fluorescence string, shape string, certificate string,location string,classify int) error { doc := Diamonds{ Certificate:certificate, // 证书 Color : color, // 颜色 Cut : cut, // 切工 Fluorescence: fluorescence, // 荧光 Neatness : neatness, // 净度 Polishing : polishing, // 抛光 Shape : shape, // 形状 Symmetric : symmetric, // 对称 Location : location, //位置 // Weight : weight, // 重量 // Price : price, // 价格 Classify : classify, // 分类 } fmt.Println(doc) result := orm.Eloquent.Table(e.TableName()).Create(&doc) if result.Error != nil { err := result.Error return err } return nil } type Msg struct { Price string // 价格 Weight string // 重量 Color string // 颜色 Neatness string // 净度 Cut string // 切工 Symmetric string // 对称 Polishing string // 抛光 Fluorescence string // 荧光 Shape string // 形状 Certificate string // 证书 CertificateNum string // 证书号 Location string // 位置 Classify int // 0为用户手动添加 1为报表导入 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Go命令官方指南【原译】发布时间:2022-07-10
下一篇:
Go语言之结构体与方法发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap