-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoexcel.go
102 lines (91 loc) · 1.88 KB
/
toexcel.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
package main
import (
"regexp"
"github.com/pkg/errors"
"github.com/hymkor/pipe2excel/internal/excel"
)
type SendCsvToExcel struct {
excel *excel.Application
sheet *excel.Sheet
book *excel.Book
row int
DoQuit bool
SaveAs string
}
func (this *SendCsvToExcel) SetDoQuit(value bool) {
this.DoQuit = value
}
func (this *SendCsvToExcel) SetSaveAs(name string) {
this.SaveAs = name
}
func NewSendCsvToExcel(visible bool) (SendCsv, error) {
excel1, err := excel.New(visible)
if err != nil {
return nil, errors.Wrap(err, "NewSendCsvToExcel")
}
book, err := excel1.NewBook()
if err != nil {
return nil, err
}
sheet, err := book.Item(1)
if err != nil {
book.Release()
return nil, err
}
return &SendCsvToExcel{
excel: excel1,
sheet: sheet,
book: book,
row: 1,
}, nil
}
func (this *SendCsvToExcel) Close() {
if this.sheet != nil {
this.sheet.Release()
this.sheet = nil
}
if this.book != nil {
if this.SaveAs != "" {
this.book.CallMethod("SaveAs", this.SaveAs)
}
this.book.Release()
this.book = nil
}
if this.excel != nil {
if this.DoQuit {
this.excel.CallMethod("Quit")
}
this.excel.Close()
this.excel = nil
}
}
func (this *SendCsvToExcel) NewSheet(name string) error {
this.sheet.Release()
s, err := this.book.Add()
if err != nil {
return err
}
this.sheet = s
s.SetName(name)
this.row = 1
return nil
}
var rxNumber = regexp.MustCompile(`^\-?[1-9]\d*(\.\d*[1-9])?$`)
func (this *SendCsvToExcel) Send(csv []string) error {
for key, val := range csv {
_cell, err := this.sheet.GetProperty("Cells", this.row, key+1)
if err != nil {
return errors.Wrap(err, "(*SendCsvToExcel)Send")
}
cell := _cell.ToIDispatch()
if rxNumber.MatchString(val) {
cell.PutProperty("NumberFormatLocal", "0_")
} else {
cell.PutProperty("NumberFormatLocal", "@")
}
cell.PutProperty("Value", val)
cell.Release()
}
this.row++
return nil
}