golang 生成excel字母表头

2021-12-15  本文已影响0人  发条家的橙子

根据下标生成类似A-Z AA-AZ的excel表头

func main() {
    fmt.Println(getKey(0))
    fmt.Println(getKey(26))
    fmt.Println(getKey(45))
}

// 根据下标 获取 对应表头
func getKey(index int) string {
    colCode := ""
    key := 'A'
    loop := index / 26
    if loop > 0 {
        colCode += getKey(loop - 1)
    }
    return colCode + string(key+int32(index)%26)
}
上一篇 下一篇

猜你喜欢

热点阅读