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 { Name string ProjectName string `export:"true,x:1,y:3"` TotalInvest Fen `export:"true,x:2,y:3"` ExData2 ExData2 CenterInvest Fen CityInvest Fen CompanyInvest Fen Partner string `export:"true,x:6,y:3"` Date time.Time `export:"x:7,y:3"` ExData []ExData } type ExData struct { Col1 string Col2 string } type ExData2 struct { CenterInvest Fen `export:"true,x:3,y:3"` CityInvest Fen `export:"true,x:4,y:3"` CompanyInvest Fen `export:"true,x:5,y:3"` } func TestMain(t *testing.T) { title := []export.Title{ { Name: "项目名称", Location: export.Location{X: 1, Y: 1}, Rowspan: 2, }, { Name: "总投资", Location: export.Location{X: 2, Y: 1}, Rowspan: 2, }, { Name: "资金来源", Location: export.Location{X: 3, Y: 1}, Colspan: 3, }, { Name: "中央", Location: export.Location{X: 3, Y: 2}, }, { Name: "地方", Location: export.Location{X: 4, Y: 2}, }, { Name: "公司", Location: export.Location{X: 5, Y: 2}, }, { Name: "合作主体", Location: export.Location{X: 6, Y: 1}, Rowspan: 2, }, { Name: "日期", Location: export.Location{X: 7, Y: 1}, Rowspan: 2, }, } data := []Data{ { Date: time.Now(), Name: "测试数据1", TotalInvest: 10000, ProjectName: "项目1", CenterInvest: 2000, CityInvest: 2000, CompanyInvest: 6000, Partner: "公司1", ExData2: ExData2{ CenterInvest: 3000, CityInvest: 1500, CompanyInvest: 5500, }, }, { Date: time.Now(), Name: "测试数据", TotalInvest: 8000, ProjectName: "项目2", CenterInvest: 2000, CityInvest: 2000, CompanyInvest: 4000, Partner: "公司2", ExData2: ExData2{ CenterInvest: 6200, CityInvest: 1200, CompanyInvest: 2600, }, }, } exporter := export.DefaultExporter() exporter.Data = &data exporter.Titles = title exporter.File = "test1.xlsx" exporter.Path = "./" // exporter.Export(0) buffer, err := exporter.ExportBuffer(0) if err != nil { fmt.Println(err) } fmt.Println(buffer) }