关于golang:Golang-操作Excel文件

34次阅读

共计 2661 个字符,预计需要花费 7 分钟才能阅读完成。

日常开发中会遇到解决 Excel 文件的相干操作,这里举荐一款利用比拟宽泛的操作 Excel 的开源工具 Excelize。

Excelize 是一个用 Go 语言编写的库,提供了一组容许您写入和读取 XLSX / XLSM / XLTM 文件的性能。反对读写由 Microsoft Excel™2007 和更高版本生成的电子表格文档。通过高度兼容性反对简单的组件,并提供了流式 API,用于从工作表中生成或读取蕴含大量数据的数据。该库须要 Go 版本 1.10 或更高版本。能够应用 go 的内置文档工具查看残缺的 API 文档,也能够在 go.dev 和 docs reference 上在线查看。

创立 Excel 文件

示例

package main

import (
    "fmt"
    "github.com/360EntSecGroup-Skylar/excelize"
)

func main() {f := excelize.NewFile()
    // Create a new sheet.
    index := f.NewSheet("Sheet2")
    // Set value of a cell.
    f.SetCellValue("Sheet2", "A2", "Hello world.")
    // 设置单元格款式
    style, err := f.NewStyle(`{
    "font":
    {
        "bold": true,
        "family": "font-family",
        "size": 20,
        "color": "#777777"
    }
}`)
    if err != nil {fmt.Println(err)
    }
    f.SetCellStyle("Sheet1", "B1", "B1", style)
    f.SetCellValue("Sheet1", "B1", "hello")

    // Set active sheet of the workbook.
    f.SetActiveSheet(index)
    // Save xlsx file by the given path.
    if err := f.SaveAs("Book1.xlsx"); err != nil {fmt.Println(err)
    }
}

插入图片到单元格

示例:

package main

import (
    "fmt"
    _ "image/gif"
    _ "image/jpeg"
    _ "image/png"

    "github.com/360EntSecGroup-Skylar/excelize"
)

func main() {f, err := excelize.OpenFile("Book1.xlsx")
    if err != nil {fmt.Println(err)
        return
    }
    // Insert a picture.
    if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil {fmt.Println(err)
    }
    // Insert a picture to worksheet with scaling.
    if err := f.AddPicture("Sheet1", "D2", "image.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {fmt.Println(err)
    }
    // Insert a picture offset in the cell with printing support.
    if err := f.AddPicture("Sheet1", "H2", "image.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`); err != nil {fmt.Println(err)
    }
    // Save the xlsx file with the origin path.
    if err = f.Save(); err != nil {fmt.Println(err)
    }
}

读取 Excel 文件

示例

package main

import (
    "fmt"

    "github.com/360EntSecGroup-Skylar/excelize"
)

func main() {f, err := excelize.OpenFile("Book1.xlsx")
    if err != nil {fmt.Println(err)
        return
    }
    // Get value from cell by given worksheet name and axis.
    cell, err := f.GetCellValue("Sheet1", "B2")
    if err != nil {fmt.Println(err)
        return
    }
    fmt.Println(cell)
    // Get all the rows in the Sheet1.
    rows, err := f.GetRows("Sheet1")
    for _, row := range rows {
        for _, colCell := range row {fmt.Print(colCell, "\t")
        }
        fmt.Println()}
}

生成 Excel 文件并下载

示例

package main

import (
    "github.com/360EntSecGroup-Skylar/excelize"
    "log"
    "net/http"
)

func down(w http.ResponseWriter, r *http.Request) {f := excelize.NewFile()
    // Set value of a cell.
    f.SetCellValue("Sheet1", "A2", "Hello world.")
    // Save xlsx file by the given path.
    //if err := f.SaveAs("Book1.xlsx"); err != nil {//    fmt.Println(err)
    //}

    w.Header().Set("Content-Type", "application/octet-stream")
    w.Header().Set("Content-Disposition", "attachment; filename="+"100 以内口算题.xlsx")
    w.Header().Set("Content-Transfer-Encoding", "binary")
    _ = f.Write(w)
}

func main() {http.HandleFunc("/", down) //   设置拜访路由
    log.Fatal(http.ListenAndServe(":8080", nil))
}

相干材料

https://github.com/360EntSecG…

https://xuri.me/excelize/zh-h…

正文完
 0