From 9e10ad4e21646a160385abd5b2dbee6dd2eaca51 Mon Sep 17 00:00:00 2001 From: gouqi11 <66834753+gouqi11@users.noreply.github.com> Date: Tue, 26 Mar 2024 14:53:56 +0800 Subject: [PATCH] feat(excelutils): support read file data (#19795) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 马鸿飞 --- pkg/util/excelutils/excelutils.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pkg/util/excelutils/excelutils.go b/pkg/util/excelutils/excelutils.go index eff8a17419..5b779b890d 100644 --- a/pkg/util/excelutils/excelutils.go +++ b/pkg/util/excelutils/excelutils.go @@ -269,3 +269,28 @@ func SetCellStyleWithColumnKey(keys []string, sheet, style string, f *excelize.F } return f, nil } + +// 按行读取excel文件,注:第一行为json的key +func ReadDataWithRow(f *excelize.File, sheet string) (jsonutils.JSONObject, error) { + datas := jsonutils.NewArray() + keys := []string{} + rows, err := f.GetRows(sheet) + if err != nil { + return nil, errors.Wrap(err, "get rows") + } + for index, row := range rows { + data := jsonutils.NewDict() + if index == 0 { + keys = row + continue + } + for i, value := range row { + if i >= len(keys) { + continue + } + data.Set(keys[i], jsonutils.NewString(value)) + } + datas.Add(data) + } + return jsonutils.Marshal(datas), nil +}