Skip to content

Commit 8be390b

Browse files
authored
修复表格标题行首尾空不匹配或多余的标题导致的SQL语句错误问题
修复表格标题行首尾空不匹配或多余的标题导致的SQL语句错误问题
1 parent 3ad83cb commit 8be390b

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

server/service/system/sys_export_template.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package system
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"mime/multipart"
89
"net/url"
@@ -398,6 +399,9 @@ func (sysExportTemplateService *SysExportTemplateService) ImportExcel(templateID
398399
if err != nil {
399400
return err
400401
}
402+
if len(rows) < 2 {
403+
return errors.New("Excel data is not enough.\nIt should contain title row and data")
404+
}
401405

402406
var templateInfoMap = make(map[string]string)
403407
err = json.Unmarshal([]byte(template.TemplateInfo), &templateInfoMap)
@@ -417,11 +421,17 @@ func (sysExportTemplateService *SysExportTemplateService) ImportExcel(templateID
417421

418422
return db.Transaction(func(tx *gorm.DB) error {
419423
excelTitle := rows[0]
424+
for i, str := range excelTitle {
425+
excelTitle[i] = strings.TrimSpace(str)
426+
}
420427
values := rows[1:]
421428
items := make([]map[string]interface{}, 0, len(values))
422429
for _, row := range values {
423430
var item = make(map[string]interface{})
424431
for ii, value := range row {
432+
if _, ok := titleKeyMap[excelTitle[ii]]; !ok {
433+
continue // excel中多余的标题,在模板信息中没有对应的字段,因此key为空,必须跳过
434+
}
425435
key := titleKeyMap[excelTitle[ii]]
426436
item[key] = value
427437
}

web/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
content="Gin,Vue,Admin.Gin-Vue-Admin,GVA,gin-vue-admin,后台管理框架,vue后台管理框架,gin-vue-admin文档,gin-vue-admin首页,gin-vue-admin"
99
name="keywords"
1010
/>
11-
<link rel="icon" href="favicon.ico" />
11+
<link rel="icon" href="/favicon.ico" />
1212
<title></title>
1313
<style>
1414
.transition-colors {

0 commit comments

Comments
 (0)