修复表格标题行首尾空不匹配或多余的标题导致的SQL语句错误问题
修复表格标题行首尾空不匹配或多余的标题导致的SQL语句错误问题
This commit is contained in:
@@ -3,6 +3,7 @@ package system
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -398,6 +399,9 @@ func (sysExportTemplateService *SysExportTemplateService) ImportExcel(templateID
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if len(rows) < 2 {
|
||||||
|
return errors.New("Excel data is not enough.\nIt should contain title row and data")
|
||||||
|
}
|
||||||
|
|
||||||
var templateInfoMap = make(map[string]string)
|
var templateInfoMap = make(map[string]string)
|
||||||
err = json.Unmarshal([]byte(template.TemplateInfo), &templateInfoMap)
|
err = json.Unmarshal([]byte(template.TemplateInfo), &templateInfoMap)
|
||||||
@@ -417,11 +421,17 @@ func (sysExportTemplateService *SysExportTemplateService) ImportExcel(templateID
|
|||||||
|
|
||||||
return db.Transaction(func(tx *gorm.DB) error {
|
return db.Transaction(func(tx *gorm.DB) error {
|
||||||
excelTitle := rows[0]
|
excelTitle := rows[0]
|
||||||
|
for i, str := range excelTitle {
|
||||||
|
excelTitle[i] = strings.TrimSpace(str)
|
||||||
|
}
|
||||||
values := rows[1:]
|
values := rows[1:]
|
||||||
items := make([]map[string]interface{}, 0, len(values))
|
items := make([]map[string]interface{}, 0, len(values))
|
||||||
for _, row := range values {
|
for _, row := range values {
|
||||||
var item = make(map[string]interface{})
|
var item = make(map[string]interface{})
|
||||||
for ii, value := range row {
|
for ii, value := range row {
|
||||||
|
if _, ok := titleKeyMap[excelTitle[ii]]; !ok {
|
||||||
|
continue // excel中多余的标题,在模板信息中没有对应的字段,因此key为空,必须跳过
|
||||||
|
}
|
||||||
key := titleKeyMap[excelTitle[ii]]
|
key := titleKeyMap[excelTitle[ii]]
|
||||||
item[key] = value
|
item[key] = value
|
||||||
}
|
}
|
||||||
|
@@ -8,7 +8,7 @@
|
|||||||
content="Gin,Vue,Admin.Gin-Vue-Admin,GVA,gin-vue-admin,后台管理框架,vue后台管理框架,gin-vue-admin文档,gin-vue-admin首页,gin-vue-admin"
|
content="Gin,Vue,Admin.Gin-Vue-Admin,GVA,gin-vue-admin,后台管理框架,vue后台管理框架,gin-vue-admin文档,gin-vue-admin首页,gin-vue-admin"
|
||||||
name="keywords"
|
name="keywords"
|
||||||
/>
|
/>
|
||||||
<link rel="icon" href="favicon.ico" />
|
<link rel="icon" href="/favicon.ico" />
|
||||||
<title></title>
|
<title></title>
|
||||||
<style>
|
<style>
|
||||||
.transition-colors {
|
.transition-colors {
|
||||||
|
Reference in New Issue
Block a user