golang获取excel中的指定列数据

获取golang中指定列数据

func ValueLoc(excel_path, sheet_name string, cols []string) [][]string{
	xlsx, err := excelize.OpenFile(excel_path)
	if err != nil {
		os.Exit(1)
		return dataframe, err
	}

	rows := xlsx.GetRows(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
}

猜你喜欢

转载自blog.csdn.net/qq_36269019/article/details/107610638