优化
This commit is contained in:
@@ -6,7 +6,9 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
@@ -59,7 +61,23 @@ func (s *ExportService) Export(ctx context.Context, tid string, params map[strin
|
||||
for rowIndex, row := range rows {
|
||||
for column, key := range keys {
|
||||
cell, _ := excelize.CoordinatesToCellName(column+1, rowIndex+2)
|
||||
_ = file.SetCellValue(sheet, cell, row[key])
|
||||
lookup := strings.ReplaceAll(strings.ReplaceAll(key, `"`, ""), "`", "")
|
||||
if len(template.Joins) > 0 {
|
||||
if parts := strings.Split(lookup, " as "); len(parts) > 1 {
|
||||
lookup = strings.TrimSpace(parts[1])
|
||||
} else if parts = strings.Split(lookup, "."); len(parts) > 1 {
|
||||
lookup = parts[1]
|
||||
}
|
||||
}
|
||||
value := fmt.Sprintf("%v", row[lookup])
|
||||
if timestamp, ok := row[lookup].(time.Time); ok {
|
||||
value = timestamp.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
if number, parseErr := strconv.ParseFloat(value, 64); parseErr == nil {
|
||||
_ = file.SetCellValue(sheet, cell, number)
|
||||
} else {
|
||||
_ = file.SetCellValue(sheet, cell, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
buffer, err := file.WriteToBuffer()
|
||||
@@ -108,11 +126,7 @@ func (s *ExportService) Import(ctx context.Context, tid string, reader io.Reader
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
sheets := file.GetSheetList()
|
||||
if len(sheets) == 0 {
|
||||
return errors.New("工作簿没有工作表")
|
||||
}
|
||||
rows, err := file.GetRows(sheets[0])
|
||||
rows, err := file.GetRows("Sheet1")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -128,6 +142,9 @@ func (s *ExportService) Import(ctx context.Context, tid string, reader io.Reader
|
||||
reverse[labels[key]] = key
|
||||
}
|
||||
headers := rows[0]
|
||||
for index := range headers {
|
||||
headers[index] = strings.TrimSpace(headers[index])
|
||||
}
|
||||
items := make([]map[string]any, 0, len(rows)-1)
|
||||
for _, row := range rows[1:] {
|
||||
item := map[string]any{}
|
||||
@@ -142,8 +159,5 @@ func (s *ExportService) Import(ctx context.Context, tid string, reader io.Reader
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err = s.uc.ImportExportRows(ctx, template, items); err != nil {
|
||||
return fmt.Errorf("导入失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
return s.uc.ImportExportRows(ctx, template, items)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user