feat:优化项目信息

This commit is contained in:
henry
2022-04-14 22:42:09 +08:00
parent 3542ff6a75
commit 8c214adf8a
2 changed files with 110 additions and 49 deletions

View File

@ -18,7 +18,7 @@ type Excel struct {
title []string title []string
content *ExcelContent content *ExcelContent
body map[string][]map[string]string body []map[string]string
} }
type ExcelContent struct { type ExcelContent struct {
@ -136,39 +136,75 @@ func (this *Excel) Export() error {
// Import 导入 // Import 导入
// @file文件信息 // @file文件信息
// @headers标题数量 // @headers头部标题数量
func (this *Excel) Import(file string, headers int) error { // @sheet读取的sheet0第一页
func (this *Excel) Import(file string, headers, sheet int) error {
_file, err := xlsx.OpenFile(file) _file, err := xlsx.OpenFile(file)
if err != nil { if err != nil {
return err return err
} }
out := make(map[string][]map[string]string, 0) if len(_file.Sheets) < sheet {
// 循环Sheet return errors.New("异常sheet")
for _, sheet := range _file.Sheets { }
data := make([]map[string]string, 0) out := make([]map[string]string, 0)
local := 0 _sheet := _file.Sheets[sheet]
if headers > 0 { local := 0
local += headers
if headers > 0 {
local += headers
}
for i := local + 1; i < len(_sheet.Rows); i++ {
body := _sheet.Rows[i].Cells
_data := make(map[string]string, 0)
for key, cell := range _sheet.Rows[local].Cells {
_data[cell.String()] = body[key].String()
} }
for i := local + 1; i < len(sheet.Rows); i++ { out = append(out, _data)
body := sheet.Rows[i].Cells
_data := make(map[string]string, 0)
for key, cell := range sheet.Rows[local].Cells {
_data[cell.String()] = body[key].String()
}
data = append(data, _data)
}
out[sheet.Name] = data
} }
this.body = out this.body = out
return nil return nil
} }
// Import 导入
// @file文件信息
// @headers标题数量
//func (this *Excel) Import(file string, headers int) error {
// _file, err := xlsx.OpenFile(file)
//
// if err != nil {
// return err
// }
// out := make(map[string][]map[string]string, 0)
// // 循环Sheet
// for _, sheet := range _file.Sheets {
// data := make([]map[string]string, 0)
//
// local := 0
//
// if headers > 0 {
// local += headers
// }
// for i := local + 1; i < len(sheet.Rows); i++ {
// body := sheet.Rows[i].Cells
// _data := make(map[string]string, 0)
//
// for key, cell := range sheet.Rows[local].Cells {
// _data[cell.String()] = body[key].String()
// }
// data = append(data, _data)
// }
// out[sheet.Name] = data
// }
// this.body = out
//
// return nil
//}
// Save 保存 // Save 保存
func (this *Excel) Save(path string) error { func (this *Excel) Save(path string) error {
return this.File.Save(this.name + ".xlsx") return this.File.Save(this.name + ".xlsx")
@ -188,37 +224,56 @@ func (this *Excel) Save(path string) error {
} }
// Analysis 解析 // Analysis 解析
func (this *Excel) Analysis(sheet string, src interface{}) error { func (this *Excel) Analysis(src interface{}) []interface{} {
for k, v := range this.body["专家信息"][0] { if src == nil {
return nil
}
out := make([]interface{}, 0)
// 原指针
_src := reflect.TypeOf(src)
tRef := reflect.TypeOf(src).Elem() // 指针类型获取真正type需要调用Elem
if _src.Kind() == reflect.Ptr {
_src = _src.Elem()
}
for _, val := range this.body {
tRef := reflect.New(_src)
fieldNum := tRef.NumField() fieldNum := tRef.Elem().NumField()
for i := 0; i < fieldNum; i++ { for k, v := range val {
// 匹配结构字段名称 for i := 0; i < fieldNum; i++ {
if k != strings.ToLower(tRef.Field(i).Tag.Get("xlsx")) { // 匹配结构字段名称
continue if k != strings.ToLower(tRef.Elem().Type().Field(i).Tag.Get("xlsx")) {
} continue
vRef := reflect.ValueOf(src).Elem().FieldByName(tRef.Field(i).Name) }
vRef := tRef.Elem().FieldByName(tRef.Elem().Type().Field(i).Name)
if !vRef.CanSet() { if !vRef.CanSet() {
continue continue
} }
switch vRef.Type().String() { switch vRef.Type().String() {
case "string": case "string":
vRef.SetString(v) vRef.SetString(v)
break break
case "int", "int64": case "int", "int32", "int64":
vRef.SetInt(utils.StringToInt64(v)) vRef.SetInt(utils.StringToInt64(v))
break break
case "uint", "uint64": case "uint", "uint32", "uint64":
vRef.SetUint(utils.StringToUnit64(v)) vRef.SetUint(utils.StringToUnit64(v))
break break
case "float", "float32", "float64":
f, _ := utils.StringToFloat(v)
vRef.SetFloat(f)
break
default:
break
}
} }
} }
out = append(out, tRef.Interface())
} }
return nil return out
} }
func NewExcel(options ...OptionExcel) *Excel { func NewExcel(options ...OptionExcel) *Excel {

View File

@ -1,6 +1,8 @@
package lib package lib
import "testing" import (
"testing"
)
func TestNewExcel(t *testing.T) { func TestNewExcel(t *testing.T) {
content := make([]interface{}, 0) content := make([]interface{}, 0)
@ -34,13 +36,17 @@ type Data struct {
func TestNewExcelImport(t *testing.T) { func TestNewExcelImport(t *testing.T) {
excel := NewExcel() excel := NewExcel()
err := excel.Import("测试.xlsx", 1) err := excel.Import("测试.xlsx", 1, 0)
if err != nil { if err != nil {
t.Log(err) t.Log(err)
return return
} }
data := new(Data) data := excel.Analysis(&Data{})
excel.Analysis("", data)
t.Log(data) for _, v := range data {
_data, ok := v.(*Data)
t.Log(ok)
t.Log(_data.Name)
}
} }