From 5e136c17498479a2ce3d38a32985e8c0f3f09a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 19 Nov 2021 16:35:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=AF=BB=E5=8F=96:=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=B0=86=E5=8D=95=E8=A1=8C=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E6=88=90map=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- excel/read.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/excel/read.go b/excel/read.go index f8d0bda..7c4a462 100644 --- a/excel/read.go +++ b/excel/read.go @@ -63,3 +63,40 @@ func (r *Read) GetAllData() (map[string][][]string, error) { } return result, nil } + +// GetAllDataToMap 读取全部数据,并返回map结构 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 4:20 下午 2021/11/19 +func (r *Read) GetAllDataToMap(fieldList []string) (map[string][]map[string]string, error) { + var ( + allData map[string][][]string + err error + formatResult map[string][]map[string]string + ) + formatResult = make(map[string][]map[string]string) + if allData, err = r.GetAllData(); nil != err { + return nil, err + } + for sheetName, sheetData := range allData { + formatResult[sheetName] = make([]map[string]string, 0) + for _, lineData := range sheetData { + tmpResult := make(map[string]string) + for idx, colData := range lineData { + if idx >= len(fieldList) { + // 指定的字段列表较短, 自动剔除后面的字段 + break + } + tmpResult[fieldList[idx]] = colData + } + // 字段列表较长, 单元格不足, 自动补齐空字符串 + for i := len(lineData); i < len(fieldList); i++ { + tmpResult[fieldList[i]] = "" + } + formatResult[sheetName] = append(formatResult[sheetName], tmpResult) + } + } + // 格式化数据 + return formatResult, nil +}