获取golang中指定列数据
func ValueLoc(excel_path, sheet_name string, cols []string) [][]string{
xlsx, err := (excel_path)
if err != nil {
(1)
return
}
rows := (sheet_name)
colIndex := make([]int, len(cols))
// 获取每个col的所在序列号
for index, row := range rows {
if index == 0 {
num := 0
for _, col := range cols {
for key, colCell := range row {
if colCell == col {
colIndex[num] = key + 1
num++
}
}
}
}
}
// 对存在的量进行重新矫正,以解决初始变量长度问题
res_len := 0
for _, coli := range colIndex {
if coli-1 >= 0 {
res_len++
}
}
// 获取数据
res_data := make([][]string, len(rows)-1)
res_index := 0
for index, row := range rows {
if index != 0 {
data := make([]string, res_len)
for i, colindex := range colIndex {
for key, colCell := range row {
if key == colindex-1 {
data[i] = colCell
}
}
}
res_data[res_index] = data
res_index++
}
}
return res_data
}