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

上一篇:Excelize -Go 开源项目中唯一支持复杂样式 XLSX 文件的类库


下一篇:visual studio C#工程设置obj文件目录