mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
feat(excelutils): support read file data (#19795)
Co-authored-by: 马鸿飞 <mahongfei@yunion.cn>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user