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
content *ExcelContent
body map[string][]map[string]string
body []map[string]string
}
type ExcelContent struct {
@ -136,39 +136,75 @@ func (this *Excel) Export() error {
// Import 导入
// @file文件信息
// @headers标题数量
func (this *Excel) Import(file string, headers int) error {
// @headers头部标题数量
// @sheet读取的sheet0第一页
func (this *Excel) Import(file string, headers, sheet 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)
if len(_file.Sheets) < sheet {
return errors.New("异常sheet")
}
out := make([]map[string]string, 0)
local := 0
_sheet := _file.Sheets[sheet]
if headers > 0 {
local += headers
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()
}
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
out = append(out, _data)
}
this.body = out
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 保存
func (this *Excel) Save(path string) error {
return this.File.Save(this.name + ".xlsx")
@ -188,37 +224,56 @@ func (this *Excel) Save(path string) error {
}
// Analysis 解析
func (this *Excel) Analysis(sheet string, src interface{}) error {
for k, v := range this.body["专家信息"][0] {
func (this *Excel) Analysis(src interface{}) []interface{} {
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++ {
// 匹配结构字段名称
if k != strings.ToLower(tRef.Field(i).Tag.Get("xlsx")) {
continue
}
vRef := reflect.ValueOf(src).Elem().FieldByName(tRef.Field(i).Name)
for k, v := range val {
for i := 0; i < fieldNum; i++ {
// 匹配结构字段名称
if k != strings.ToLower(tRef.Elem().Type().Field(i).Tag.Get("xlsx")) {
continue
}
vRef := tRef.Elem().FieldByName(tRef.Elem().Type().Field(i).Name)
if !vRef.CanSet() {
continue
}
switch vRef.Type().String() {
case "string":
vRef.SetString(v)
break
case "int", "int64":
vRef.SetInt(utils.StringToInt64(v))
break
case "uint", "uint64":
vRef.SetUint(utils.StringToUnit64(v))
break
if !vRef.CanSet() {
continue
}
switch vRef.Type().String() {
case "string":
vRef.SetString(v)
break
case "int", "int32", "int64":
vRef.SetInt(utils.StringToInt64(v))
break
case "uint", "uint32", "uint64":
vRef.SetUint(utils.StringToUnit64(v))
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 {

View File

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