Skip to content

Commit a17118d

Browse files
committed
init
0 parents  commit a17118d

File tree

7 files changed

+991
-0
lines changed

7 files changed

+991
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.exe
2+
.exe~

converters/csv.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package converters
2+
3+
import (
4+
"encoding/csv"
5+
"encoding/json"
6+
"io"
7+
"log"
8+
"os"
9+
"strconv"
10+
)
11+
12+
func ConvertCSVToJSON(filepath string, hasHeaders bool) []byte {
13+
// Open the CSV file
14+
file, err := os.Open(filepath)
15+
if err != nil {
16+
log.Fatal(err)
17+
}
18+
defer file.Close()
19+
20+
// Create a CSV reader
21+
reader := csv.NewReader(file)
22+
23+
var header []string
24+
25+
if hasHeaders {
26+
header, _ = reader.Read()
27+
}
28+
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
var objects []map[string]string
33+
for {
34+
row, err := reader.Read()
35+
if err == io.EOF {
36+
break
37+
} else if err != nil {
38+
log.Fatal(err)
39+
}
40+
obj := make(map[string]string)
41+
for i, value := range row {
42+
if hasHeaders {
43+
obj[header[i]] = value
44+
} else {
45+
obj[strconv.Itoa(i+1)] = value
46+
}
47+
}
48+
49+
objects = append(objects, obj)
50+
}
51+
jsonData, err := json.Marshal(objects)
52+
if err != nil {
53+
log.Fatal(err)
54+
}
55+
56+
return jsonData
57+
}

converters/excel.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package converters
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"strconv"
8+
9+
"github.com/xuri/excelize/v2"
10+
)
11+
12+
func ConvertExcelToJSON(filepath string, hasHeaders bool) []byte {
13+
f, err := excelize.OpenFile(filepath)
14+
if err != nil {
15+
fmt.Println(err)
16+
return nil
17+
}
18+
19+
defer func() {
20+
if err := f.Close(); err != nil {
21+
fmt.Println(err)
22+
}
23+
}()
24+
25+
var objects []map[string]string
26+
var headers []string
27+
28+
rows, err := f.GetRows("Sheet1")
29+
30+
if hasHeaders {
31+
headers = rows[0]
32+
}
33+
34+
if err != nil {
35+
return nil
36+
}
37+
obj := make(map[string]string)
38+
for _, row := range rows {
39+
for i, colCell := range row {
40+
if hasHeaders {
41+
headerCell := headers[i]
42+
obj[headerCell] = colCell
43+
} else {
44+
obj[strconv.Itoa(i+1)] = colCell
45+
}
46+
}
47+
objects = append(objects, obj)
48+
}
49+
jsonData, err := json.Marshal(objects)
50+
if err != nil {
51+
log.Fatal(err)
52+
}
53+
return jsonData
54+
}

go-excel.exe~

31.7 MB
Binary file not shown.

go.mod

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module github.com/charliescript/go-excel
2+
3+
go 1.20
4+
5+
require (
6+
fyne.io/fyne/v2 v2.3.5
7+
github.com/xuri/excelize/v2 v2.7.1
8+
)
9+
10+
require (
11+
fyne.io/systray v1.10.1-0.20230602210930-b6a2d6ca2a7b // indirect
12+
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/fredbi/uri v0.1.0 // indirect
14+
github.com/fsnotify/fsnotify v1.5.4 // indirect
15+
github.com/fyne-io/gl-js v0.0.0-20220119005834-d2da28d9ccfe // indirect
16+
github.com/fyne-io/glfw-js v0.0.0-20220120001248-ee7290d23504 // indirect
17+
github.com/fyne-io/image v0.0.0-20220602074514-4956b0afb3d2 // indirect
18+
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 // indirect
19+
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
20+
github.com/go-text/typesetting v0.0.0-20230405155246-bf9c697c6e16 // indirect
21+
github.com/godbus/dbus/v5 v5.1.0 // indirect
22+
github.com/goki/freetype v0.0.0-20220119013949-7a161fd3728c // indirect
23+
github.com/gopherjs/gopherjs v1.17.2 // indirect
24+
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect
25+
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
26+
github.com/pmezard/go-difflib v1.0.0 // indirect
27+
github.com/richardlehane/mscfb v1.0.4 // indirect
28+
github.com/richardlehane/msoleps v1.0.3 // indirect
29+
github.com/srwiley/oksvg v0.0.0-20220731023508-a61f04f16b76 // indirect
30+
github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect
31+
github.com/stretchr/testify v1.8.0 // indirect
32+
github.com/tevino/abool v1.2.0 // indirect
33+
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 // indirect
34+
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect
35+
github.com/yuin/goldmark v1.4.13 // indirect
36+
golang.org/x/crypto v0.8.0 // indirect
37+
golang.org/x/image v0.5.0 // indirect
38+
golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee // indirect
39+
golang.org/x/net v0.9.0 // indirect
40+
golang.org/x/sys v0.7.0 // indirect
41+
golang.org/x/text v0.9.0 // indirect
42+
gopkg.in/yaml.v3 v3.0.1 // indirect
43+
honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2 // indirect
44+
)

0 commit comments

Comments
 (0)