Skip to content

Commit

Permalink
docs: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
VarusHsu committed Aug 14, 2024
1 parent 4fd0ab2 commit 11c5c9c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
Binary file added foo.xlsx
Binary file not shown.
58 changes: 30 additions & 28 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,51 @@ a easier use excel file create tool for golang
## Quick Start
* define a struct with excel_header tag and implement `SheetName` method
```go
type User struct {
Name string `excel_header:"姓名"`
Age int `excel_header:"年龄"`
Birthday time.Time `excel_header:"生日"`
Jobs *string `excel_header:"工作"`
type Foo struct {
ID int64 `excel_header:"id"`
Name string `excel_header:"name"`
CreatedAt time.Time `excel_header:"created_at"`
DeletedAt *time.Time `excel_header:"deleted_at"`
}
func (u User) SheetName() string {
return "用户信息"
func (u Foo) SheetName() string {
return "foo sheet name"
}
```

* construct some data
```go
user1 := User{
Name: "张三",
Age: 18,
Birthday: time.Now(),
Jobs: nil,
bar1DeletedAt := time.Date(2024, 1, 3, 15, 4, 5, 0, time.Local)
sheetModels := []excelorm.SheetModel{
Foo{
ID: 1,
Name: "Bar1",
CreatedAt: time.Date(2024, 1, 2, 15, 4, 5, 0, time.Local),
DeletedAt: &bar1DeletedAt,
},
Foo{
ID: 2,
Name: "Bar2",
CreatedAt: time.Date(2024, 1, 2, 15, 4, 5, 0, time.Local),
},
}
user2 := User{
Name: "李四",
Age: 20,
Birthday: time.Now(),
Jobs: toPtr("程序员"),
}
sheetModels := make([]excelorm.SheetModel, 0)
sheetModels = append(sheetModels, user1, user2)
```
* write to excel file
```go
err := excelorm.WriteExcelSaveAs("test.xlsx", sheetModels)
if err != nil {
panic(err)
if err := excelorm.WriteExcelSaveAs("foo.xlsx", sheetModels,
excelorm.WithTimeFormatLayout("2006/01/02 15:04:05"),
excelorm.WithIfNullValue("-"),
); err != nil {
log.Fatal(err)
}
```
* you can see the result in the file<br>

| 姓名 | 年龄 | 生日 | 工作 |
|----|----|---------------------|-----|
| 张三 | 18 | 2021-08-08 16:00:00 | |
| 李四 | 20 | 2021-08-08 16:00:00 | 程序员 |
| id | name | created_at | deleted_at |
|----|------|---------------------|---------------------|
| 1 | Bar1 | 2024/01/02 15:04:05 | 2024/01/03 15:04:05 |
| 2 | Bar2 | 2024/01/02 15:04:05 | - |


[test.xlsx](test.xlsx)
[foo.xlsx](foo.xlsx)

* support multi-sheets by define more structs
Binary file removed test.xlsx
Binary file not shown.

0 comments on commit 11c5c9c

Please sign in to comment.