-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (122 loc) · 3.43 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
145
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/360EntSecGroup-Skylar/excelize"
)
type User struct {
FirstName string `json:"firstname"`
LastName string `json:"lastName"`
Email string `json:"email"`
}
func exportCSV(user []User) {
file, err := os.Create("user.csv")
if err != nil {
fmt.Println("Error creating CSV file:", err)
return
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
header := []string{"firstname", "lastName", "email"}
if err := writer.Write(header); err != nil {
fmt.Println("Error writing CSV header:", err)
return
}
for _, person := range user {
row := []string{person.FirstName, person.LastName, person.Email}
if err := writer.Write(row); err != nil {
fmt.Println("Error writing CSV data:", err)
return
}
}
fmt.Println("CSV file 'user.csv' has been created in the current directory.")
}
func exportXLSX(user []User) {
xlsx := excelize.NewFile()
sheetName := "Sheet1"
xlsx.SetSheetName(xlsx.GetSheetName(1), sheetName)
xlsx.SetCellValue(sheetName, "A1", "firstname")
xlsx.SetCellValue(sheetName, "B1", "lastName")
xlsx.SetCellValue(sheetName, "C1", "email")
for i, person := range user {
rowIndex := i + 2
xlsx.SetCellValue(sheetName, fmt.Sprintf("A%d", rowIndex), person.FirstName)
xlsx.SetCellValue(sheetName, fmt.Sprintf("B%d", rowIndex), person.LastName)
xlsx.SetCellValue(sheetName, fmt.Sprintf("C%d", rowIndex), person.Email)
}
if err := xlsx.SaveAs("user.xlsx"); err != nil {
fmt.Println("Error saving XLSX file:", err)
return
}
fmt.Println("XLSX file 'user.xlsx' has been created in the current directory.")
}
func exportXML(user []User) {
xmlData := `<user>`
for _, person := range user {
xmlData += fmt.Sprintf(
`<person><firstname>%s</firstname><lastName>%s</lastName><email>%s</email></person>`,
person.FirstName, person.LastName, person.Email,
)
}
xmlData += `</user>`
file, err := os.Create("user.xml")
if err != nil {
fmt.Println("Error creating XML file:", err)
return
}
defer file.Close()
_, err = file.WriteString(xmlData)
if err != nil {
fmt.Println("Error writing XML data:", err)
return
}
fmt.Println("XML file 'user.xml' has been created in the current directory.")
}
func exportHTML(user []User) {
htmlData := `<html><head><title>People Data</title></head><body><table><tr><th>First Name</th><th>Last Name</th><th>Email</th></tr>`
for _, person := range user {
htmlData += fmt.Sprintf(
`<tr><td>%s</td><td>%s</td><td>%s</td></tr>`,
person.FirstName, person.LastName, person.Email,
)
}
htmlData += `</table></body></html>`
file, err := os.Create("user.html")
if err != nil {
fmt.Println("Error creating HTML file:", err)
return
}
defer file.Close()
_, err = file.WriteString(htmlData)
if err != nil {
fmt.Println("Error writing HTML data:", err)
return
}
fmt.Println("HTML file 'user.html' has been created in the current directory.")
}
func exportToFile(input, exportType string) {
var user []User
if err := json.Unmarshal([]byte(input), &user); err != nil {
fmt.Println("Error unmarshaling JSON:", err)
return
}
exportType = strings.ToLower(exportType)
switch exportType {
case "csv":
exportCSV(user)
case "xlsx":
exportXLSX(user)
case "xml":
exportXML(user)
case "html":
exportHTML(user)
}
}
func main() {
jsonData := `[{"firstname" : "Alex", "lastName" : "Mat", "email" : "[email protected]"}]`
exportToFile(jsonData, "csv")
}