(c *gin.Context)
| 14 | ) |
| 15 | |
| 16 | func ImportChannels(c *gin.Context) { |
| 17 | // 从请求中获取上传的文件 |
| 18 | file, err := c.FormFile("file") |
| 19 | if err != nil { |
| 20 | common.ApiError(c, fmt.Errorf("failed to get file: %w", err)) |
| 21 | return |
| 22 | } |
| 23 | |
| 24 | // 打开文件 |
| 25 | src, err := file.Open() |
| 26 | if err != nil { |
| 27 | common.ApiError(c, fmt.Errorf("failed to open file: %w", err)) |
| 28 | return |
| 29 | } |
| 30 | defer src.Close() |
| 31 | |
| 32 | // 读取Excel文件 |
| 33 | f, err := excelize.OpenReader(src) |
| 34 | if err != nil { |
| 35 | common.ApiError(c, fmt.Errorf("failed to open Excel file: %w", err)) |
| 36 | return |
| 37 | } |
| 38 | defer func() { |
| 39 | if err := f.Close(); err != nil { |
| 40 | common.SysError("Error closing Excel file: " + err.Error()) |
| 41 | } |
| 42 | }() |
| 43 | |
| 44 | // 获取第一个工作表 |
| 45 | sheetName := f.GetSheetName(0) |
| 46 | if sheetName == "" { |
| 47 | common.ApiError(c, fmt.Errorf("no sheets found in Excel file")) |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | // 读取所有行 |
| 52 | rows, err := f.GetRows(sheetName) |
| 53 | if err != nil { |
| 54 | common.ApiError(c, fmt.Errorf("failed to read rows: %w", err)) |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | // 检查是否有数据 |
| 59 | if len(rows) <= 1 { |
| 60 | common.ApiError(c, fmt.Errorf("no data found in Excel file")) |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | // 解析表头 |
| 65 | headers := rows[0] |
| 66 | headerMap := make(map[string]int) |
| 67 | for i, header := range headers { |
| 68 | headerMap[header] = i |
| 69 | } |
| 70 | |
| 71 | // 解析数据行 |
| 72 | channels := make([]model.Channel, 0, len(rows)-1) |
| 73 | for i := 1; i < len(rows); i++ { |
nothing calls this directly
no test coverage detected