提交 8c63c7c5 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

export data

上级 110bf2f0
package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/easysoft/zendata/cmd/test/import/comm"
"github.com/easysoft/zendata/cmd/test/import/model"
fileUtils "github.com/easysoft/zendata/pkg/utils/file"
"path/filepath"
"reflect"
)
func main() {
filePath := "data/city/v1.xlsx"
sheetName := "city"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
db := comm.GetDB()
db.AutoMigrate(
&model.DataCity{},
)
pos := make([]model.DataCity, 0)
db.Where("NOT deleted").Find(&pos)
f := excelize.NewFile()
index := f.NewSheet(sheetName)
var infos []model.TableInfo
db.Raw("desc " + model.DataCity{}.TableName()).Scan(&infos)
excelColNameArr, excelColNameHeader := comm.GetExcelColsByTableDef(infos)
fieldNames := comm.GetStructFields(model.DataCity{})
// gen headers
for index, name := range excelColNameHeader {
excelColName := excelColNameArr[index]
excelColId := fmt.Sprintf("%s%d", excelColName, 1)
f.SetCellValue(sheetName, excelColId, name)
}
// gen rows
for rowIndex, po := range pos {
for fieldIndex, fieldName := range fieldNames {
val := ""
if fieldName == "Id" {
val = fmt.Sprintf("%d", reflect.ValueOf(po).FieldByName(fieldName).Int())
} else {
val = reflect.ValueOf(po).FieldByName(fieldName).String()
}
excelColName := excelColNameArr[fieldIndex]
excelColId := fmt.Sprintf("%s%d", excelColName, rowIndex+2)
f.SetCellValue(sheetName, excelColId, val)
}
}
f.SetActiveSheet(index)
f.SaveAs(filePath)
}
package comm
import (
"fmt"
"github.com/easysoft/zendata/cmd/test/import/model"
"reflect"
)
func GetStructFields(interf interface{}) (fieldNames []string) {
retH := reflect.TypeOf(interf)
for i := 0; i < retH.NumField(); i++ {
f := retH.Field(i)
if f.Name != "BaseModel" {
fieldNames = append(fieldNames, f.Name)
}
}
return
}
func GetExcelColsByTableDef(infos []model.TableInfo) (excelColNameArr, excelColNameHeader []string) {
excelColIndex := 'A'
for _, info := range infos {
if info.Field == "created_at" || info.Field == "updated_at" || info.Field == "deleted" || info.Field == "disabled" {
continue
}
colName := fmt.Sprintf("%c", excelColIndex)
excelColNameArr = append(excelColNameArr, colName)
excelColNameHeader = append(excelColNameHeader, info.Field)
excelColIndex += 1
}
return
}
......@@ -6,7 +6,6 @@ const (
CreateTableTempl = `CREATE TABLE IF NOT EXISTS %s (
id bigint auto_increment,
content varchar(1000) not null unique,
tag varchar(50),
primary key(id)
) engine=innodb default charset=utf8 auto_increment=1;`
......
......@@ -52,7 +52,7 @@ func (DataCountry) TableName() string {
}
type DataCity struct {
BaseModel
Id int `json:"id"`
Name string `json:"name"`
Code string `json:"code"`
......@@ -194,3 +194,21 @@ type DataWord struct {
func (DataWord) TableName() string {
return "biz_data_word"
}
type DataContent struct {
BaseModel
Content string `json:"content"`
}
func (DataContent) TableName() string {
return "biz_data_word"
}
type TableInfo struct {
Field string
Type string
Null string
Key string
Default string
Extra string
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册