zoukankan      html  css  js  c++  java
  • go gin 读取excel 插入Mysql

    代码如下:

    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 ", 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为报表导入 }
  • 相关阅读:
    C++官方文档-静态成员
    C++官方文档-this
    C++官方文档-运算符重载
    springboot-dokcer
    HDU 1073
    HDU 1070
    UVa 213
    HDU 1150
    POJ 1274
    POJ 2594
  • 原文地址:https://www.cnblogs.com/ztshuai/p/13861060.html
Copyright © 2011-2022 走看看