-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
119 lines (113 loc) · 2.41 KB
/
export.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
package main
import (
"encoding/base64"
"encoding/csv"
"fmt"
_ "image/png"
"os"
"strings"
"time"
wails "github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/xuri/excelize/v2"
)
type ExportData struct {
Header []string
Data [][]interface{}
Image string
}
func (b *App) Export(exportType string, data *ExportData) string {
var err error
switch exportType {
case "excel":
err = b.exportExcel(data)
case "csv":
err = b.exportCSV(data)
default:
return "not suppoerted"
}
if err != nil {
OutLog("ExportTable err=%v", err)
return fmt.Sprintf("export err=%v", err)
}
return ""
}
func (b *App) exportExcel(data *ExportData) error {
d := time.Now().Format("20060102150405")
file, err := wails.SaveFileDialog(b.ctx, wails.SaveDialogOptions{
DefaultFilename: "TWLogAIAN_Export_Log_" + d + ".xlsx",
CanCreateDirectories: true,
Filters: []wails.FileFilter{{
DisplayName: "Excel",
Pattern: "*.xlsx",
}},
})
if err != nil {
return err
}
if file == "" {
return nil
}
f := excelize.NewFile()
f.SetCellValue("Sheet1", "A1", "TWLogAIAN Export Log "+d)
row := 3
col := 'A'
for _, h := range data.Header {
f.SetCellValue("Sheet1", fmt.Sprintf("%c%d", col, row), h)
col++
}
imgCol := col + 2
row++
for _, l := range data.Data {
col = 'A'
for _, i := range l {
f.SetCellValue("Sheet1", fmt.Sprintf("%c%d", col, row), i)
col++
}
row++
}
if data.Image != "" {
b64data := data.Image[strings.IndexByte(data.Image, ',')+1:]
img, err := base64.StdEncoding.DecodeString(b64data)
if err != nil {
return err
}
f.AddPictureFromBytes("Sheet1", fmt.Sprintf("%c2", imgCol), "", "TWLogAIANLog", ".png", img)
}
if err := f.SaveAs(file); err != nil {
return err
}
return nil
}
func (b *App) exportCSV(data *ExportData) error {
d := time.Now().Format("20060102150405")
file, err := wails.SaveFileDialog(b.ctx, wails.SaveDialogOptions{
DefaultFilename: "TWLogAIAN_Export_Log_" + d + ".csv",
CanCreateDirectories: true,
Filters: []wails.FileFilter{{
DisplayName: "CSV",
Pattern: "*.csv",
}},
})
if err != nil {
return err
}
if file == "" {
return nil
}
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
w := csv.NewWriter(f)
w.Write(data.Header)
for _, l := range data.Data {
data := []string{}
for _, i := range l {
data = append(data, fmt.Sprintf("%v", i))
}
w.Write(data)
}
w.Flush()
return w.Error()
}