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

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

创立Excel文件

示例

package mainimport (    "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 mainimport (    "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 mainimport (    "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 mainimport (    "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...