mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 18:52:56 +00:00
hot fix #1
This commit is contained in:
@@ -4,11 +4,14 @@ import (
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
// ExportHelper provides export utilities for CSV, JSON, etc.
|
||||
@@ -111,7 +114,10 @@ func (eh *ExportHelper) formatFieldValue(v reflect.Value) string {
|
||||
}
|
||||
}
|
||||
|
||||
// ImportFromCSV imports data from CSV file
|
||||
// ImportFromCSV imports tabular data from an uploaded file.
|
||||
// It supports traditional CSV files as well as Excel workbooks (.xlsx).
|
||||
// The caller always receives a [][]string where the first row is treated
|
||||
// as the header row by higher-level import functions.
|
||||
func (eh *ExportHelper) ImportFromCSV(c *gin.Context, formFieldName string) ([][]string, error) {
|
||||
file, err := c.FormFile(formFieldName)
|
||||
if err != nil {
|
||||
@@ -124,6 +130,28 @@ func (eh *ExportHelper) ImportFromCSV(c *gin.Context, formFieldName string) ([][
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(file.Filename))
|
||||
if ext == ".xlsx" {
|
||||
wb, err := excelize.OpenReader(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
_ = wb.Close()
|
||||
}()
|
||||
|
||||
sheets := wb.GetSheetList()
|
||||
if len(sheets) == 0 {
|
||||
return nil, fmt.Errorf("xlsx file has no sheets")
|
||||
}
|
||||
|
||||
rows, err := wb.GetRows(sheets[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
reader := csv.NewReader(f)
|
||||
records, err := reader.ReadAll()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user