Golang 操做Excel文件

平常开发中会遇处处理Excel文件的相关操做,这里推荐一款应用比较普遍的操做Excel的开源工具Excelize。git

Excelize是一个用Go语言编写的库,提供了一组容许您写入和读取XLSX / XLSM / XLTM文件的功能。支持读写由Microsoft Excel™2007和更高版本生成的电子表格文档。经过高度兼容性支持复杂的组件,并提供了流式API,用于从工做表中生成或读取包含大量数据的数据。该库须要Go版本1.10或更高版本。能够使用go的内置文档工具查看完整的API文档,也能够在go.devdocs reference上在线查看github

建立Excel文件

示例app

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)
    }
}

插入图片到单元格工具

示例:excel

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文件

示例code

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...开发

相关文章
相关标签/搜索