提交 0ebea09e 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

import data from excel

上级 1710d1f8
......@@ -6,6 +6,7 @@ import (
"github.com/fatih/color"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/plugin/dbresolver"
"time"
)
......@@ -44,7 +45,7 @@ func GetDB() *gorm.DB {
&model.DataCity{},
&model.DataColor{},
&model.DataColor{},
&model.DataCountry{},
&model.DataChineseFamily{},
&model.DataChineseGiven{},
&model.DataEnglishFamily{},
......@@ -68,7 +69,10 @@ func GormMySQL() *gorm.DB {
DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
SkipInitializeWithVersion: false, // 根据版本自动配置
}
if db, err := gorm.Open(mysql.New(mysqlConfig)); err != nil {
if db, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
}); err != nil {
return nil
} else {
sqlDB, _ := db.DB()
......
package main
import (
"github.com/easysoft/zendata/src/test/import/comm"
"github.com/easysoft/zendata/src/test/import/model"
stringUtils "github.com/easysoft/zendata/src/utils/string"
)
func main() {
filePath := "data/country/v1.xlsx"
sheetName := "country"
db := comm.GetDB()
records := comm.GetExcelTable(filePath, sheetName)
for _, record := range records {
po := model.DataCountry{
ContinentId: stringUtils.ParseInt(record["continent_id"].(string)),
Continent: record["continent"].(string),
AreaCode: record["areacode"].(string),
EnglishShort: record["enshort"].(string),
EnglishFull: record["enfull"].(string),
ChineseShort: record["cnshort"].(string),
ChineseFull: record["cnfull"].(string),
}
db.Save(&po)
}
}
......@@ -38,6 +38,7 @@ type BaseModel struct {
type DataCountry struct {
BaseModel
ContinentId int `json:"continentId"`
Continent string `json:"continent"`
AreaCode string `json:"areaCode"`
EnglishShort string `json:"englishShort"`
......
......@@ -288,3 +288,9 @@ func ConvertYamlStringToMapFormat(bytes []byte) (ret string) {
//}
return
}
func ParseInt(str string) (ret int) {
ret, _ = strconv.Atoi(str)
return
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册