Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/go-the-way/exl
Excel binding to struct written in Go.(Only supports Go1.18+)
https://github.com/go-the-way/exl
Last synced: 14 days ago
JSON representation
Excel binding to struct written in Go.(Only supports Go1.18+)
- Host: GitHub
- URL: https://github.com/go-the-way/exl
- Owner: go-the-way
- License: other
- Created: 2022-04-19T06:04:31.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-04T02:32:48.000Z (5 months ago)
- Last Synced: 2024-07-31T20:52:21.154Z (3 months ago)
- Language: Go
- Homepage:
- Size: 83 KB
- Stars: 29
- Watchers: 0
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - exl - Excel binding to struct written in Go.(Only supports Go1.18+) (Microsoft Office / Microsoft Excel)
- zero-alloc-awesome-go - exl - Excel binding to struct written in Go.(Only supports Go1.18+) (Microsoft Office / Microsoft Excel)
- awesome-go-extra - exl - 04-19T06:04:31Z|2022-06-24T09:24:36Z| (Microsoft Office / Microsoft Excel)
README
# exl
Excel binding to struct written in Go.(Only supports Go1.18+)[![CircleCI](https://circleci.com/gh/go-the-way/exl/tree/main.svg?style=shield)](https://circleci.com/gh/go-the-way/exl/tree/main)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/go-the-way/exl)
[![codecov](https://codecov.io/gh/go-the-way/exl/branch/main/graph/badge.svg?token=8MAR3J959H)](https://codecov.io/gh/go-the-way/exl)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-the-way/exl)](https://goreportcard.com/report/github.com/go-the-way/exl)
[![GoDoc](https://pkg.go.dev/badge/github.com/go-the-way/exl?status.svg)](https://pkg.go.dev/github.com/go-the-way/exl?tab=doc)
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#microsoft-excel)## usage
### Read Excel
```go
package mainimport (
"fmt"
"github.com/go-the-way/exl"
)type ReadExcel struct {
ID int `excel:"ID"`
Name string `excel:"Name"`
}func (*ReadExcel) ReadConfigure(rc *exl.ReadConfig) {}
func main() {
if models, err := exl.ReadFile[*ReadExcel]("/to/path.xlsx"); err != nil {
fmt.Println("read excel err:" + err.Error())
} else {
fmt.Printf("read excel num: %d\n", len(models))
}
}
```### Write Excel
```go
package mainimport (
"fmt"
"github.com/go-the-way/exl"
)type WriteExcel struct {
ID int `excel:"ID"`
Name string `excel:"Name"`
}func (m *WriteExcel) WriteConfigure(wc *exl.WriteConfig) {}
func main() {
if err := exl.Write("/to/path.xlsx", []*WriteExcel{{100, "apple"}, {200, "pear"}}); err != nil {
fmt.Println("write excel err:" + err.Error())
} else {
fmt.Println("write excel done")
}
}
```## Writer
```go
package mainimport (
"fmt""github.com/go-the-way/exl"
)func main() {
w := exl.NewWriter()
if err := w.Write("int", []int{1, 2}); err != nil {
fmt.Println(err)
return
}
if err := w.Write("float", []float64{1.1, 2.2}); err != nil {
fmt.Println(err)
return
}
if err := w.Write("string", []string{"hello", "world"}); err != nil {
fmt.Println(err)
return
}
if err := w.Write("map", []map[string]string{{"id":"1000","name":"hello"},{"id":"2000","name":"world"}}); err != nil {
fmt.Println(err)
return
}
if err := w.Write("structWithField", []struct{ID int}{{1000},{2000}}); err != nil {
fmt.Println(err)
return
}
if err := w.Write("structWithTag", []struct{ID int `excel:"编号"`}{{1000},{2000}}); err != nil {
fmt.Println(err)
return
}
if err := w.Write("structWithTagAndIgnore", []struct{
ID int `excel:"编号"`
Extra int `excel:"-"`
Name string `excel:"名称"`
}{{1000,0,"Coco"},{2000,0,"Apple"}}); err != nil {
fmt.Println(err)
return
}
if err := w.SaveTo("dist.xlsx"); err != nil {
fmt.Println(err)
return
}
}
```