-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (137 loc) · 3.03 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/csv"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/tealeg/xlsx"
"github.com/urfave/cli"
)
var (
output string
trim bool
trimFloat bool
withBom bool
convertBool bool
fillCell bool
)
func main() {
app := cli.NewApp()
app.Name = "excel2csv"
app.Usage = "convert excel each sheets to a single csv"
app.UsageText = "excel2csv [--output DIR] [--trim] [--trim-float] [--with-bom] file [file...]"
app.Version = "0.0.11"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "output, o",
Value: ".",
Usage: "target directory for output csv",
Destination: &output,
},
cli.BoolFlag{
Name: "trim",
Usage: "trim value",
Destination: &trim,
},
cli.BoolFlag{
Name: "fill-cell",
Usage: "fill cell",
Destination: &fillCell,
},
cli.BoolFlag{
Name: "trim-float",
Usage: "try to parse string like 1.10000000000001 to 1.1",
Destination: &trimFloat,
},
cli.BoolFlag{
Name: "convert-bool",
Usage: "covert 0 1 to \"true\" \"false\"",
Destination: &convertBool,
},
cli.BoolFlag{
Name: "with-bom",
Usage: "add UTF-8 BOM to csv file",
Destination: &withBom,
},
}
app.Action = convert
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func convert(c *cli.Context) error {
for _, arg := range c.Args() {
if err := convertExcelTo(arg); err != nil {
return err
}
}
return nil
}
func convertExcelTo(filePath string) error {
xlFile, err := xlsx.OpenFile(filePath)
if err != nil {
return errors.New(fmt.Sprintf("open xlsx file %s failed: %s", filePath, err))
}
var errors []error
for _, sheet := range xlFile.Sheets {
if err := convertSheetTo(sheet); err != nil {
errors = append(errors, err)
}
}
for _, err := range errors {
fmt.Printf("convert %s has failed: %s", filePath, err)
}
return nil
}
func convertSheetTo(sheet *xlsx.Sheet) error {
csvName := sheet.Name + ".csv"
csvPath := filepath.Join(output, csvName)
f, err := os.OpenFile(csvPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0664)
if err != nil {
return err
}
defer f.Close()
log.Printf("convert %s into %s", sheet.Name, csvPath)
if withBom {
_, err := f.Write(bomBytes)
if err != nil {
return err
}
}
w := csv.NewWriter(f)
w.UseCRLF = false
var maxRecords int
for _, row := range sheet.Rows {
var records []string
for _, cell := range row.Cells {
record := cell.String()
if convertBool && cell.Type() == xlsx.CellTypeBool {
record = boolStringToCharacter(cell.String())
}
if trimFloat {
record = roundFloat(record)
}
if trim {
record = strings.TrimSpace(record)
}
records = append(records, record)
}
if fillCell {
if count := len(records); count > maxRecords {
maxRecords = count
} else if dif := maxRecords - count; dif > 0 {
for i := 0; i < dif; i++ {
records = append(records, "")
}
}
}
if err := w.Write(records); err != nil {
return err
}
}
w.Flush()
return w.Error()
}