forked from miaomiao3/xlsxutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.go
108 lines (98 loc) · 2.36 KB
/
csv.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package xlsxutil
import (
"bufio"
"bytes"
"errors"
"os"
"reflect"
"strings"
)
// CsvDump dump data in csv format
// data: ptr of slice, slice element should be a struct
// sep: separator of csv line. be careful to avoid value conflict
func CsvDump(sep string, data interface{}) (*bytes.Buffer, error) {
dataValue := reflect.ValueOf(data)
if dataValue.Kind() != reflect.Slice {
return nil, errors.New("do not support non slice data")
}
sliceLen := dataValue.Len()
if sliceLen == 0 {
return nil, errors.New("empty slice")
}
var optionMap map[string]*xlsOption
buf := &bytes.Buffer{}
var err error
for i := 0; i < sliceLen; i++ {
itemValue := dataValue.Index(i)
if itemValue.Kind() == reflect.Ptr {
itemValue = itemValue.Elem()
}
if i == 0 {
var rowStrSegs []string
optionMap, err = addHeaderRow(itemValue, func(str string, kind reflect.Kind) {
rowStrSegs = append(rowStrSegs, str)
})
if err != nil {
return nil, err
}
buf.WriteString(getLineFromRowSegs(rowStrSegs, sep) + "\n")
}
var rowStrSegs []string
err = addRow(itemValue, optionMap, func(str string, kind reflect.Kind) {
rowStrSegs = append(rowStrSegs, str)
})
if err != nil {
return nil, err
}
buf.WriteString(getLineFromRowSegs(rowStrSegs, sep) + "\n")
}
return buf, nil
}
func getLineFromRowSegs(row []string, sep string) string {
return strings.Join(row, sep)
}
// CsvLoad load csv data
// data: pointer of a slice
func CsvLoad(fileName string, sep string, data interface{}) error {
file, err := os.Open(fileName)
if err != nil {
return err
}
defer file.Close()
setter, err := NewSliceSetter(data)
if err != nil {
return (err)
}
headerMap := make(map[int]string)
scanner := bufio.NewScanner(file)
lineCnt := 0
for scanner.Scan() {
lineCnt++
if lineCnt == 1 {
rowStr := scanner.Text()
rowStrs := strings.Split(rowStr, sep)
for columnIndex, cell := range rowStrs {
if len(cell) == 0 { // if value is empty, ignore
continue
}
headerMap[columnIndex] = cell
}
continue
}
rowStr := scanner.Text()
if rowStr == "" {
continue
}
valueMap := make(map[string]string)
rowStrSegs := strings.Split(rowStr, sep)
for k, v := range rowStrSegs {
if len(headerMap[k]) == 0 { // if head is empty, ignore
continue
}
valueMap[headerMap[k]] = v
}
setter.AddElement(valueMap)
}
setter.Update()
return err
}