-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexcel2csv.go
121 lines (106 loc) · 2.78 KB
/
excel2csv.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
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"github.com/tealeg/xlsx"
)
type Export struct {
FromFile string
ToFile string
StartRow int
StartCol int
SheetName string
FieldNames []string
ExcelColumns []string
}
func NewExport(fromfile, tofile, sheetname string) *Export {
return &Export{
FromFile: fromfile,
ToFile: tofile,
SheetName: sheetname,
}
}
func (export *Export) Export() {
//Open xlsx file for reading
xlFile, error := xlsx.OpenFile(export.FromFile)
if error != nil {
fmt.Println("File Error:", error)
return
}
sheet := xlFile.Sheet[export.SheetName]
if sheet == nil {
log.Fatalf("Could not find sheet '%s' in excel workbook.", export.SheetName)
return
}
var table [][]string
var numberOfCols int
numberOfCols = 0
if len(export.FieldNames) > 0 {
table = append(table, export.FieldNames)
numberOfCols = len(export.FieldNames)
if len(export.ExcelColumns) == 0 {
export.ExcelColumns = make([]string, numberOfCols)
for n := 0; n < numberOfCols; n++ {
export.ExcelColumns[n] = xlsx.ColIndexToLetters(n + export.StartCol)
}
}
} else if len(export.ExcelColumns) > 0 {
numberOfCols = len(export.ExcelColumns)
export.FieldNames = export.ExcelColumns
} else {
numberOfCols = len(sheet.Cols)
}
fmt.Println("Columns Found : ", numberOfCols)
fmt.Println("Excel Columns : ", len(sheet.Cols))
if len(export.FieldNames) != len(export.ExcelColumns) {
log.Fatalln("Field names and excel columns must contain the same number of fields.")
return
}
for rowi, row := range sheet.Rows {
if rowi < export.StartRow {
continue
}
if numberOfCols == 0 {
numberOfCols = len(row.Cells)
}
if len(row.Cells) > export.StartCol {
tablerow := make([]string, numberOfCols)
table = append(table, tablerow)
// for coli, cell := range row.Cells[export.StartCol : export.StartCol+numberOfCols] {
// tablerow[coli] = cell.Value
// }
for coli := 0; coli < numberOfCols; coli++ {
cellIndex := coli
if len(export.ExcelColumns) > 0 {
cellIndex = xlsx.ColLettersToIndex(export.ExcelColumns[coli])
}
if cellIndex >= len(row.Cells) {
fmt.Printf("Row %v Column %v ignored, out of index rage %v, max columns %v \r\n", rowi+1, export.ExcelColumns[coli], cellIndex, len(row.Cells))
continue
}
cell := row.Cells[cellIndex]
if cell.IsTime() {
t1, err := cell.GetTime(false)
if err != nil {
tablerow[coli] = fmt.Sprint("%s", err)
} else {
tablerow[coli] = t1.Format("2006-01-02 15:04")
}
} else {
tablerow[coli] = cell.Value
}
}
}
}
//Create csv file for export.
csvfile, err := os.Create(export.ToFile)
defer csvfile.Close()
if err != nil {
fmt.Println(err)
return
}
writer := csv.NewWriter(csvfile)
writer.WriteAll(table)
}