-
Notifications
You must be signed in to change notification settings - Fork 7
/
excel.go
42 lines (40 loc) · 1.08 KB
/
excel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package excel2json
import (
"bytes"
"github.com/360EntSecGroup-Skylar/excelize"
"strings"
"time"
)
// parseExcelFileData to read all data excel
// data is byte data from http client
// keyName is string concat
// sheetName is string
func parseExcelFileData(data []byte, keyName, sheetName string) ([]*map[string]interface{}, error) {
var (
headers []string
result []*map[string]interface{}
wb = new(excelize.File)
err error
)
// if already data in cache
if cacheData, found := localCache.Get(keyName); found {
return cacheData.([]*map[string]interface{}), nil
}
// open byte data with reader
if wb, err = excelize.OpenReader(bytes.NewReader(data)); err != nil {
return nil, err
}
// Get all the rows in the Sheet.
rows := wb.GetRows(sheetName)
headers = rows[0]
for _, row := range rows[1:] {
var tmpMap = make(map[string]interface{})
for j, v := range row {
tmpMap[strings.Join(strings.Split(headers[j], " "), "")] = v
}
result = append(result, &tmpMap)
}
// set data to cache
localCache.Set(keyName, result, 10*time.Minute)
return result, nil
}