-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel.go
81 lines (64 loc) · 1.38 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
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
package excel
import (
"errors"
"io"
"os"
"path/filepath"
"github.com/360EntSecGroup-Skylar/excelize"
)
// New new excel backend
func New(value interface{}, config ...*Config) *Excel {
excel := &Excel{}
if f, ok := value.(string); ok {
excel.filename = f
} else {
if r, ok := value.(io.ReadCloser); ok {
excel.reader = r
}
if w, ok := value.(io.WriteCloser); ok {
excel.writer = w
}
}
for _, cfg := range config {
excel.config = cfg
}
if excel.config == nil {
excel.config = &Config{}
}
return excel
}
// Config excel config
type Config struct {
TrimSpace bool
SheetName string
}
// Excel excel struct
type Excel struct {
filename string
reader io.ReadCloser
writer io.WriteCloser
config *Config
}
func (excel *Excel) getReader() (io.ReadCloser, error) {
if excel.reader != nil {
return excel.reader, nil
} else if excel.filename != "" {
readerCloser, err := os.Open(excel.filename)
return readerCloser, err
}
return nil, errors.New("Nothing available to import")
}
func (excel *Excel) getWriter() (*excelize.File, error) {
if excel.filename != "" {
dir := filepath.Dir(excel.filename)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, os.ModePerm)
}
f := excelize.NewFile()
return f, nil
}
if excel.writer != nil {
return excelize.NewFile(), nil
}
return nil, errors.New("Nowhere to export")
}