1 Star 0 Fork 0

nggs / util

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
excel.go 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
lwj8507 提交于 2018-05-01 20:35 . util的xlsx改名成excel
package util
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"github.com/tealeg/xlsx"
)
func DeserializeStructFromExcelRow(e interface{}, row *xlsx.Row) (err error) {
v := reflect.ValueOf(e)
ve := v.Elem()
if ve.Kind() != reflect.Struct {
return fmt.Errorf("[%#v] is not a struct", e)
}
t := reflect.TypeOf(e)
te := t.Elem()
for i := 0; i < ve.NumField(); i++ {
column := i + 1
tf := te.Field(i)
tagColumn := tf.Tag.Get("excel_column")
if tagColumn != "" {
if column, err = strconv.Atoi(tagColumn); err != nil {
return fmt.Errorf("get %s column fail, %s", tf.Name, err)
}
}
if column >= len(row.Cells) {
continue
}
cell := row.Cells[column]
if cell.Value == "" {
continue
}
vef := ve.Field(i)
kind := vef.Kind()
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i64, err := cell.Int64()
if err != nil {
return fmt.Errorf("could not set %s %s to %s, %s", kind.String(), tf.Name, cell.Value, err)
}
vef.SetInt(i64)
case reflect.String:
vef.SetString(cell.String())
case reflect.Float32, reflect.Float64:
f64, err := cell.Float()
if err != nil {
return fmt.Errorf("could not set %s %s to %s, %s", kind.String(), tf.Name, cell.Value, err)
}
vef.SetFloat(f64)
case reflect.Slice, reflect.Struct, reflect.Array:
if err := json.Unmarshal([]byte(cell.String()), vef.Addr().Interface()); err != nil {
return fmt.Errorf("could not set %s %s to %s, %s", kind.String(), tf.Name, cell.Value, err)
}
default:
if err := json.Unmarshal([]byte(cell.String()), vef.Interface()); err != nil {
return fmt.Errorf("could not set %s %s to %s, %s", kind.String(), tf.Name, cell.Value, err)
}
}
}
return nil
}
Go
1
https://gitee.com/nggs/util.git
git@gitee.com:nggs/util.git
nggs
util
util
master

搜索帮助