App下載

如何使用 Go 語言在電子表格中生成相關(guān)下拉列表?案例代碼分享!

萌癌蘿莉 2021-09-17 15:49:00 瀏覽數(shù) (3530)
反饋

Excelize 是一個(gè)用純 Go 編寫的庫,提供了一組允許您寫入和讀取 XLSX / XLSM / XLTM 文件的函數(shù)。支持讀取和寫入 Microsoft Excel? 2007 及更高版本生成的電子表格文檔。以高兼容性支持復(fù)雜組件,并提供流式API,用于從海量數(shù)據(jù)的工作表中生成或讀取數(shù)據(jù)。這個(gè)庫需要 Go 1.15 或更高版本。

使用 Go 使用 Excelize 在電子表格中創(chuàng)建相關(guān)下拉列表,如下所示:

相關(guān)下拉列表gif。

Go:

package main
import (
    "fmt"
    "github.com/xuri/excelize/v2"
)
func main() {
    // create a new spreadsheet
    f := excelize.NewFile()
    var (
        // cell values
        data = [][]interface{}{
            {"Fruits", "Vegetables"},
            {"Mango", "Potato", nil, "Drop Down 1", "Drop Down 2"},
            {"Apple", "Tomato"},
            {"Grapes", "Spinach"},
            {"Strawberry", "Onion"},
            {"Kiwi", "Cucumber"},
        }
        addr                    string
        err                     error
        cellsStyle, headerStyle int
    )
    // set each cell value
    for r, row := range data {
        if addr, err = excelize.JoinCellName("A", r+1); err != nil {
            fmt.Println(err)
            return
        }
        if err = f.SetSheetRow("Sheet1", addr, &row); err != nil {
            fmt.Println(err)
            return
        }
    }
    // set data validation
    dvRange1 := excelize.NewDataValidation(true)
    dvRange1.Sqref = "D3:D3"
    dvRange1.SetSqrefDropList("$A$1:$B$1", true)
    if err = f.AddDataValidation("Sheet1", dvRange1); err != nil {
        fmt.Println(err)
        return
    }
    dvRange2 := excelize.NewDataValidation(true)
    dvRange2.Sqref = "E3:E3"
    dvRange2.SetSqrefDropList("INDIRECT(D3)", true)
    if err = f.AddDataValidation("Sheet1", dvRange2); err != nil {
        fmt.Println(err)
        return
    }
    // set defined name
    if err = f.SetDefinedName(&excelize.DefinedName{
        Name:     "Fruits",
        RefersTo: "Sheet1!$A$2:$A$6",
        Scope:    "Sheet1",
    }); err != nil {
        fmt.Println(err)
        return
    }
    if err = f.SetDefinedName(&excelize.DefinedName{
        Name:     "Vegetables",
        RefersTo: "Sheet1!$B$2:$B$6",
        Scope:    "Sheet1",
    }); err != nil {
        fmt.Println(err)
        return
    }
    // set custom column width
    for col, width := range map[string]float64{
        "A": 12, "B": 12, "C": 6, "D": 12, "E": 12} {
        if err = f.SetColWidth("Sheet1", col, col, width); err != nil {
            fmt.Println(err)
            return
        }
    }
    // hide gridlines for the worksheet
    if err = f.SetSheetViewOptions("Sheet1", 0,
        excelize.ShowGridLines(false)); err != nil {
        fmt.Println(err)
        return
    }
 // define the border style
    border := []excelize.Border{
        {Type: "top", Style: 1, Color: "cccccc"},
        {Type: "left", Style: 1, Color: "cccccc"},
        {Type: "right", Style: 1, Color: "cccccc"},
        {Type: "bottom", Style: 1, Color: "cccccc"},
    }
    // define the style of cells
    if cellsStyle, err = f.NewStyle(&excelize.Style{
        Font:   &excelize.Font{Color: "333333"},
        Border: border}); err != nil {
        fmt.Println(err)
        return
    }
    // define the style of the header row
    if headerStyle, err = f.NewStyle(&excelize.Style{
        Font: &excelize.Font{Bold: true},
        Fill: excelize.Fill{
            Type: "pattern", Color: []string{"dae9f3"}, Pattern: 1},
        Border: border},
    ); err != nil {
        fmt.Println(err)
        return
    }
    // set cell style
    if err = f.SetCellStyle("Sheet1", "A2", "B6", cellsStyle); err != nil {
        fmt.Println(err)
        return
    }
    if err = f.SetCellStyle("Sheet1", "D3", "E3", cellsStyle); err != nil {
        fmt.Println(err)
        return
    }
    // set cell style for the header row
    if err = f.SetCellStyle("Sheet1", "A1", "B1", headerStyle); err != nil {
        fmt.Println(err)
        return
    }
    if err = f.SetCellStyle("Sheet1", "D2", "E2", headerStyle); err != nil {
        fmt.Println(err)
        return
    }
    // save spreadsheet file
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }

}


0 人點(diǎn)贊