其他分享
首页 > 其他分享> > golang excel 操作

golang excel 操作

作者:互联网

只是一个简单的记录

参考代码

package main
import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {
    categories := map[string]string{"A2": "小", "A3": "正常", "A4": "大", "B1": "苹果", "C1": "橘子", "D1": "梨"}
    values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
    f := excelize.NewFile()
    for k, v := range categories {
        f.SetCellValue("Sheet1", k, v)
    }
    for k, v := range values {
        f.SetCellValue("Sheet1", k, v)
    }
    if err := f.AddChart("Sheet1", "E1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"水果3D图"}}`); err != nil {
        fmt.Println(err)
        return
    }
    // Save spreadsheet by the given path.
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

说明

golang 操作office 的包比较少,对于excel 操作来说excelize 是一个很不错的选择

参考资料

https://github.com/360EntSecGroup-Skylar/excelize

标签:name,err,values,excel,golang,Sheet1,excelize,操作,categories
来源: https://www.cnblogs.com/rongfengliang/p/14149017.html