golang excel 导入mysql

package main

import (
   "fmt"
   "github.com/xuri/excelize"
   "log"
   "reflect"
   "strconv"
   "time"
)

type ExcelData interface {
   CreateMap(arr []string) map[string]interface{}
   ChangeTime(source string) time.Time
}
type ExcelStrcut struct {
   temp  [][]string
   Model interface{}
   Info  []map[string]string
}
type CacrmCaCrmUserHelper struct {
   JobCode       string
   Password    string
   Code string
   Name string
   TypeName string
   Type string
   BackCode string
}
var file *excelize.File
func (excel *ExcelStrcut) ReadExcel(stringArray [][]string) *ExcelStrcut {
   excel.temp = stringArray
   return excel

}

func (excel *ExcelStrcut) CreateMap() *ExcelStrcut {
   //利用反射得到字段名
   for _, v := range excel.temp {
      var info = make(map[string]string)
      for i := 0; i < reflect.ValueOf(excel.Model).NumField(); i++ {

         obj := reflect.TypeOf(excel.Model).Field(i)
         //fmt.Printf("key:%s--val:%s\n",obj.Name,v[i])
         info[obj.Name] = v[i]
      }
      excel.Info = append(excel.Info, info)
   }
   return excel
}
func (excel *ExcelStrcut) ChangeTime(source string) time.Time {
   ChangeAfter, err := time.Parse("2006-01-02", source)
   if err != nil {
      log.Fatalf("转换时间错误:%s", err)
   }
   return ChangeAfter
}
func main() {
   f, err := excelize.OpenFile("test.xlsx")
   if err != nil {
      fmt.Println(err)
      return
   }
   excel_map:=f.GetSheetMap()
   for i,k:=range excel_map{
      rows, err := f.GetRows(k)
      if err != nil {
         fmt.Println(err)
         return
      }
      e:=ExcelStrcut{}
      temp := CacrmCaCrmUserHelper{}
      e.Model=temp
      e.ReadExcel(rows).CreateMap().SaveDb(i,&temp)
   }
}
func (excel *ExcelStrcut)SaveDb(j int,temp *CacrmCaCrmUserHelper) *ExcelStrcut{

   //忽略标题行
   for i:=1 ;i 
  

 

 

db

package main
import (
   "encoding/json"
   "fmt"
   "github.com/go-sql-driver/mysql"
   "github.com/jinzhu/gorm"
   "io/ioutil"
   "log"
   "os"
)
type Config struct {
   Db            struct {
      Host        string            `json:"host"`
      Port        string            `json:"port"`
      User        string            `json:"user"`
      Password    string            `json:"password"`
      Name        string            `json:"name"`
      Prefix      string            `json:"prefix"`
      MaxOpenConn int               `json:"max_open_conn"`
      Params      map[string]string `json:"params"`
      Debug       bool              `json:"debug"`
   } `json:"db"`
}

var instanceDb *gorm.DB
var DefaultConfig *Config
func NewDb()(*gorm.DB, error){
   if instanceDb==nil{
      config:=NewConfig("test.json")
      fmt.Println(config)
      mysqlConfig := mysql.NewConfig()
      mysqlConfig.User = config.Db.User
      mysqlConfig.DBName = config.Db.Name
      mysqlConfig.Passwd = config.Db.Password
      mysqlConfig.Params = config.Db.Params
      mysqlConfig.Net = "tcp"
      mysqlConfig.Addr = config.Db.Host + ":" + config.Db.Port
      db, err := gorm.Open("mysql", mysqlConfig.FormatDSN())
      db.DB().SetMaxOpenConns(config.Db.MaxOpenConn)
      db.SingularTable(true)
      instanceDb = db
      return instanceDb, err
   }
   return instanceDb, nil
}
func NewConfig(file string) *Config {
   if DefaultConfig==nil{
      configText,err:=ioutil.ReadFile(file)
      if err!=nil{
         log.Println("配置文件读取错误,启动默认配置:", err.Error())
         os.Exit(1)
      }
      err=json.Unmarshal(configText,&DefaultConfig)
      return DefaultConfig
   }
   return DefaultConfig
}

你可能感兴趣的:(golang,mysql)