mirror of
https://github.com/quarkcloudio/quark-go.git
synced 2025-09-27 12:22:11 +08:00
chore: 优化excel导入导出
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/template/resource/types"
|
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/template/resource/types"
|
||||||
"github.com/quarkcloudio/quark-go/v2/pkg/builder"
|
"github.com/quarkcloudio/quark-go/v2/pkg/builder"
|
||||||
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
||||||
|
"github.com/quarkcloudio/quark-go/v2/pkg/utils/excel"
|
||||||
"github.com/xuri/excelize/v2"
|
"github.com/xuri/excelize/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ func (p *ExportRequest) Handle(ctx *builder.Context) error {
|
|||||||
Elem().
|
Elem().
|
||||||
FieldByName("Label").
|
FieldByName("Label").
|
||||||
String()
|
String()
|
||||||
f.SetCellValue("Sheet1", p.generateColumnLabel(fieldKey+1)+"1", Label)
|
f.SetCellValue("Sheet1", excel.GenerateColumnLabel(fieldKey+1)+"1", Label)
|
||||||
}
|
}
|
||||||
|
|
||||||
for dataKey, dataValue := range data.([]interface{}) {
|
for dataKey, dataValue := range data.([]interface{}) {
|
||||||
@@ -85,7 +86,7 @@ func (p *ExportRequest) Handle(ctx *builder.Context) error {
|
|||||||
rowData[name] = dataValue.(map[string]interface{})[name]
|
rowData[name] = dataValue.(map[string]interface{})[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
f.SetCellValue("Sheet1", p.generateColumnLabel(filedKey+1)+strconv.Itoa(dataKey+2), rowData[name])
|
f.SetCellValue("Sheet1", excel.GenerateColumnLabel(filedKey+1)+strconv.Itoa(dataKey+2), rowData[name])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,15 +233,3 @@ func (p *ExportRequest) performsList(ctx *builder.Context, lists []map[string]in
|
|||||||
// 导出前回调
|
// 导出前回调
|
||||||
return template.BeforeExporting(ctx, result)
|
return template.BeforeExporting(ctx, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成 Excel 中的列标签
|
|
||||||
func (p *ExportRequest) generateColumnLabel(index int) (colLabel string) {
|
|
||||||
|
|
||||||
for index > 0 {
|
|
||||||
mod := (index - 1) % 26
|
|
||||||
colLabel = string(rune('A'+mod)) + colLabel
|
|
||||||
index = (index - 1) / 26
|
|
||||||
}
|
|
||||||
|
|
||||||
return colLabel
|
|
||||||
}
|
|
||||||
|
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/template/resource/types"
|
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/template/resource/types"
|
||||||
"github.com/quarkcloudio/quark-go/v2/pkg/builder"
|
"github.com/quarkcloudio/quark-go/v2/pkg/builder"
|
||||||
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
"github.com/quarkcloudio/quark-go/v2/pkg/dal/db"
|
||||||
|
"github.com/quarkcloudio/quark-go/v2/pkg/utils/excel"
|
||||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/file"
|
"github.com/quarkcloudio/quark-go/v2/pkg/utils/file"
|
||||||
"github.com/quarkcloudio/quark-go/v2/pkg/utils/rand"
|
"github.com/quarkcloudio/quark-go/v2/pkg/utils/rand"
|
||||||
"github.com/xuri/excelize/v2"
|
"github.com/xuri/excelize/v2"
|
||||||
@@ -86,7 +87,6 @@ func (p *ImportRequest) Handle(ctx *builder.Context, indexRoute string) error {
|
|||||||
|
|
||||||
// 解析字段
|
// 解析字段
|
||||||
for _, item := range lists {
|
for _, item := range lists {
|
||||||
|
|
||||||
// 获取表单数据
|
// 获取表单数据
|
||||||
formValues := p.transformFormValues(fields, item)
|
formValues := p.transformFormValues(fields, item)
|
||||||
|
|
||||||
@@ -166,18 +166,14 @@ func (p *ImportRequest) Handle(ctx *builder.Context, indexRoute string) error {
|
|||||||
|
|
||||||
// 创建表头
|
// 创建表头
|
||||||
importHead = append(importHead, "错误信息")
|
importHead = append(importHead, "错误信息")
|
||||||
a := 'a'
|
|
||||||
for i := 1; i <= len(importHead); i++ {
|
for i := 1; i <= len(importHead); i++ {
|
||||||
f.SetCellValue("Sheet1", string(a)+"1", importHead[i-1])
|
f.SetCellValue("Sheet1", excel.GenerateColumnLabel(i)+"1", importHead[i-1])
|
||||||
a++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建数据
|
// 创建数据
|
||||||
for k, v := range importFailedData {
|
for k, v := range importFailedData {
|
||||||
a := 'a'
|
|
||||||
for i := 1; i <= len(v); i++ {
|
for i := 1; i <= len(v); i++ {
|
||||||
f.SetCellValue("Sheet1", string(a)+strconv.Itoa(k+2), v[i-1])
|
f.SetCellValue("Sheet1", excel.GenerateColumnLabel(i)+strconv.Itoa(k+2), v[i-1])
|
||||||
a++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/rule"
|
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/component/form/rule"
|
||||||
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/template/resource/types"
|
"github.com/quarkcloudio/quark-go/v2/pkg/app/admin/template/resource/types"
|
||||||
"github.com/quarkcloudio/quark-go/v2/pkg/builder"
|
"github.com/quarkcloudio/quark-go/v2/pkg/builder"
|
||||||
|
"github.com/quarkcloudio/quark-go/v2/pkg/utils/excel"
|
||||||
"github.com/xuri/excelize/v2"
|
"github.com/xuri/excelize/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,14 +40,10 @@ func (p *ImportTemplateRequest) Handle(ctx *builder.Context) error {
|
|||||||
// 创建一个工作表
|
// 创建一个工作表
|
||||||
index, _ := f.NewSheet("Sheet1")
|
index, _ := f.NewSheet("Sheet1")
|
||||||
|
|
||||||
//定义一个字符 变量a 是一个byte类型的 表示单个字符
|
// 创建表头
|
||||||
var a = 'a'
|
|
||||||
|
|
||||||
//生成26个字符
|
|
||||||
for i := 1; i <= len(exportTitles); i++ {
|
for i := 1; i <= len(exportTitles); i++ {
|
||||||
// 设置单元格的值
|
// 设置单元格的值
|
||||||
f.SetCellValue("Sheet1", string(a)+"1", exportTitles[i-1])
|
f.SetCellValue("Sheet1", excel.GenerateColumnLabel(i)+"1", exportTitles[i-1])
|
||||||
a++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置工作簿的默认工作表
|
// 设置工作簿的默认工作表
|
||||||
|
15
pkg/utils/excel/excel.go
Normal file
15
pkg/utils/excel/excel.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package excel
|
||||||
|
|
||||||
|
// 生成 Excel 中的列标签
|
||||||
|
func GenerateColumnLabel(index int) string {
|
||||||
|
|
||||||
|
var columnLabel string
|
||||||
|
|
||||||
|
for index > 0 {
|
||||||
|
mod := (index - 1) % 26
|
||||||
|
columnLabel = string(rune('A'+mod)) + columnLabel
|
||||||
|
index = (index - 1) / 26
|
||||||
|
}
|
||||||
|
|
||||||
|
return columnLabel
|
||||||
|
}
|
Reference in New Issue
Block a user