//go:build example // +build example package main import ( "fmt" "log" "os" "time" "git.toowon.com/jimmy/go-common/excel" "git.toowon.com/jimmy/go-common/factory" "git.toowon.com/jimmy/go-common/tools" ) type User struct { ID int `json:"id"` Name string `json:"name"` Email string `json:"email"` CreatedAt time.Time `json:"created_at"` Status int `json:"status"` } func main() { app := factory.New(nil) ex := app.Excel() users := []User{ {ID: 1, Name: "Alice", Email: "alice@example.com", CreatedAt: time.Now(), Status: 1}, {ID: 2, Name: "Bob", Email: "bob@example.com", CreatedAt: time.Now(), Status: 0}, } columns := []excel.ExportColumn{ {Header: "ID", Field: "ID", Width: 10}, {Header: "姓名", Field: "Name", Width: 20}, {Header: "邮箱", Field: "Email", Width: 30}, { Header: "创建时间", Field: "CreatedAt", Format: excel.AdaptTimeFormatter(tools.FormatDateTime), }, { Header: "状态", Field: "Status", Format: func(value interface{}) string { if status, ok := value.(int); ok && status == 1 { return "启用" } return "禁用" }, }, } if err := ex.ExportToFile("users.xlsx", "用户列表", columns, users); err != nil { log.Fatal(err) } fmt.Println("exported users.xlsx") f, err := os.Create("users_http.xlsx") if err != nil { log.Fatal(err) } defer f.Close() if err := ex.ExportToWriter(f, "用户列表", columns, users); err != nil { log.Fatal(err) } fmt.Println("exported users_http.xlsx") }