关于go:go语言解析excel

31次阅读

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

go get github.com/xuri/excelize/v2

package main

import (
    "fmt"
    "strings"

    "github.com/xuri/excelize/v2"
)

func main() {f, err := excelize.OpenFile("/Users/test/abc.xlsx")
    if err != nil {fmt.Println(err)
        return
    }
    rows, err := f.GetRows("Sheet1")
    if err != nil {fmt.Println(err)
        return
    }
    fmt.Println("rows:", len(rows))
    var sql strings.Builder
    sql.WriteString("INSERT INTO test (a, b, c, d, e) VALUES\n")
    for idx, row := range rows {
        if idx == 0 {continue}
        var a, b, c, d string
        for colIdx, colCell := range row {
            switch colIdx {
            case 0:
                a = colCell
            case 1:
                b = colCell
            case 2:
                c = colCell
            case 4:
                d = colCell
            }
        }
        sql.WriteString("('" + a + "','" + b + "','" + c + "','" + d + "','2023-01-10 00:00:00'), \n")
    }
    fmt.Println(sql.String())

}

正文完
 0