现在我假设所有其他事情(如创建,读取等)都超出了这个问题的范围,并且您有一个具有当前excel文件的结构。
在这种情况下,您可以尝试以下操作:file
func GetCol(f *excelize.File, sheet string, col rune) ([]string, error) {
colIndex := int(col) - 'A'
cols, err := f.GetCols(sheet)
if err != nil {
return nil, err
}
if colIndex >= len(cols) {
return nil, errors.New("column index out of bound")
}
return cols[colIndex], nil
}
并按如下方式使用它:
cols, _ := GetCol(f, "Sheet1", 'B')
for _, col := range cols {
fmt.Println(col)
}
确保使用大写列索引。您可以修改函数以直接获取索引。另外,请确保以自己的方式处理错误。