This commit is contained in:
Tomas Dvorak
2026-01-26 08:13:18 +01:00
parent aa036b6550
commit dfc079288f
505 changed files with 95755 additions and 5712 deletions
+29 -1
View File
@@ -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 {