ð exl - Awesome Go Library for Microsoft Office
Excel binding to struct written in Go.(Only supports Go1.18+)
ð·ïļ Microsoft Office
ð Libraries for working with Microsoft Excel.
â 30 stars
Detailed Description of exl
exl
Excel binding to struct written in Go.(Only supports Go1.18+)
usage
Read Excel
package main
import (
"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
package main
import (
"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
package main
import (
"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
}
}