Golang读取表格指定行列数据

By | 2022-01-18
package main

import(
    "fmt"
    "github.com/tealeg/xlsx"
    "os"
    "strconv"
)

var (
    inFile = os.Args[1]
)

func main(){
    hang, _ := strconv.Atoi(os.Args[2])
    lie, _ := strconv.Atoi(os.Args[3])
    xlFile, err := xlsx.OpenFile(inFile)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    for _, sheet := range xlFile.Sheets {
        //遍历行读取
        for l, row := range sheet.Rows {
            // 遍历每行的列读取
            for c, cell := range row.Cells {
                text := cell.String()
                if l==hang-1 && c==lie-1 {
                  fmt.Println(text)
                }

            }
        }
    }
}

用法

//读取一行一列的数据
go run main.go filename 1 1

打包好的程序

https://github.com/HewieWang/Readxlsx/releases/tag/1.0