fix bug time struct
parent
dcb536dc16
commit
b52dfc1b1d
|
@ -76,9 +76,16 @@ func (e *Exporter) Export(sheetIndex int) error {
|
|||
func (e *Exporter) dealElement(sheet string, data reflect.Value, index int) error {
|
||||
for i := 0; i < data.NumField(); i++ {
|
||||
field := data.Field(i)
|
||||
t := data.Type().Field(i)
|
||||
tag, ok := t.Tag.Lookup("export")
|
||||
switch field.Kind() {
|
||||
case reflect.Struct:
|
||||
e.dealElement(sheet, field, index)
|
||||
// 如果是结构体类型,但是没有export标签,则进行递归,否则直接处理(例如time.Time类型是结构体,但是可能需要导出)
|
||||
if !ok {
|
||||
e.dealElement(sheet, field, index)
|
||||
} else {
|
||||
e.writeCell(sheet, field, tag, index)
|
||||
}
|
||||
// 该逻辑有待验证
|
||||
case reflect.Slice:
|
||||
for j := 0; j < field.Len(); j++ {
|
||||
|
@ -88,32 +95,64 @@ func (e *Exporter) dealElement(sheet string, data reflect.Value, index int) erro
|
|||
}
|
||||
}
|
||||
default:
|
||||
t := data.Type().Field(i)
|
||||
tag, ok := t.Tag.Lookup("export")
|
||||
if ok {
|
||||
tagMap := dealTag(tag)
|
||||
x, _ := strconv.Atoi(tagMap["x"])
|
||||
y, _ := strconv.Atoi(tagMap["y"])
|
||||
colSpan, _ := strconv.Atoi(tagMap["colspan"])
|
||||
rowSpan, _ := strconv.Atoi(tagMap["rowspan"])
|
||||
location := Location{
|
||||
X: x,
|
||||
Y: y + index,
|
||||
}
|
||||
cell := Cell{
|
||||
Location: location,
|
||||
Rowspan: rowSpan,
|
||||
Colspan: colSpan,
|
||||
Value: field,
|
||||
}
|
||||
e.SetCell(sheet, cell)
|
||||
e.writeCell(sheet, field, tag, index)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return e.Save()
|
||||
}
|
||||
|
||||
func (e *Exporter) writeCell(sheet string, field reflect.Value, tag string, index int) {
|
||||
tagMap := dealTag(tag)
|
||||
x, _ := strconv.Atoi(tagMap["x"])
|
||||
y, _ := strconv.Atoi(tagMap["y"])
|
||||
colSpan, _ := strconv.Atoi(tagMap["colspan"])
|
||||
rowSpan, _ := strconv.Atoi(tagMap["rowspan"])
|
||||
location := Location{
|
||||
X: x,
|
||||
Y: y + index,
|
||||
}
|
||||
value := value(field)
|
||||
cell := Cell{
|
||||
Location: location,
|
||||
Rowspan: rowSpan,
|
||||
Colspan: colSpan,
|
||||
Value: value,
|
||||
}
|
||||
e.SetCell(sheet, cell)
|
||||
}
|
||||
|
||||
func value(field reflect.Value) (val string) {
|
||||
mthV := field.MethodByName("String")
|
||||
if !mthV.IsValid() && field.CanAddr() {
|
||||
mthV = field.Addr().MethodByName("String")
|
||||
}
|
||||
if mthV.IsValid() && mthV.Kind() == reflect.Func && mthV.Type().NumIn() == 0 {
|
||||
callV := mthV.Call(nil)
|
||||
|
||||
if len(callV) > 0 {
|
||||
val = fmt.Sprintf("%s", callV[0].String())
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
if field.CanUint() {
|
||||
val = fmt.Sprintf("%d", field.Uint())
|
||||
|
||||
} else if field.CanInt() {
|
||||
val = fmt.Sprintf("%d", field.Int())
|
||||
|
||||
} else if field.CanFloat() {
|
||||
val = fmt.Sprintf("%f", field.Float())
|
||||
|
||||
} else {
|
||||
val = fmt.Sprintf("%s", field.String())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func dealTag(tag string) map[string]string {
|
||||
tag_list := strings.Split(tag, ",")
|
||||
tag_map := make(map[string]string)
|
||||
|
|
|
@ -1,14 +1,55 @@
|
|||
package xlsx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.botann.com/lijun/xlsx/export"
|
||||
)
|
||||
|
||||
type Fen int
|
||||
|
||||
func (t *Fen) Value() float64 {
|
||||
return float64(*t) * 0.01
|
||||
}
|
||||
|
||||
func (t *Fen) String() string {
|
||||
return fmt.Sprintf("%.2f", t.Value())
|
||||
}
|
||||
|
||||
func (t Fen) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(t.String())
|
||||
}
|
||||
|
||||
func (t *Fen) UnmarshalJSON(data []byte) error {
|
||||
y := float64(0)
|
||||
err := json.Unmarshal(data, &y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = Fen(math.Round(y * 100))
|
||||
return nil
|
||||
}
|
||||
|
||||
type Date time.Time
|
||||
|
||||
func (t *Date) Value() time.Time {
|
||||
return time.Time(*t)
|
||||
}
|
||||
|
||||
func (t *Date) String() string {
|
||||
return t.Value().Format("2006-01-02")
|
||||
}
|
||||
|
||||
func (t Date) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(t.String())
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
Date time.Time `export:"x:7,y:3"`
|
||||
Name string
|
||||
TotalInvest Fen `export:"true,x:2,y:3"`
|
||||
ProjectName string `export:"true,x:1,y:3"`
|
||||
|
@ -75,9 +116,16 @@ func Test(t *testing.T) {
|
|||
Colspan: 1,
|
||||
Rowspan: 0,
|
||||
},
|
||||
{
|
||||
Name: "日期",
|
||||
Location: export.Location{X: 7, Y: 1},
|
||||
Colspan: 1,
|
||||
Rowspan: 0,
|
||||
},
|
||||
}
|
||||
data := []Data{
|
||||
{
|
||||
Date: time.Now(),
|
||||
Name: "测试数据1",
|
||||
TotalInvest: 10000,
|
||||
ProjectName: "项目1",
|
||||
|
@ -85,10 +133,6 @@ func Test(t *testing.T) {
|
|||
CityInvest: 2000,
|
||||
CompanyInvest: 6000,
|
||||
Partner: "公司1",
|
||||
ExData: []ExData{
|
||||
{Col1: "test1", Col2: "test2"},
|
||||
{Col1: "测试1", Col2: "测试2"},
|
||||
},
|
||||
ExData2: ExData2{
|
||||
CenterInvest: 3000,
|
||||
CityInvest: 1500,
|
||||
|
@ -96,6 +140,7 @@ func Test(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
Date: time.Now(),
|
||||
Name: "测试数据",
|
||||
TotalInvest: 8000,
|
||||
ProjectName: "项目2",
|
||||
|
|
Loading…
Reference in New Issue