-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
101 lines (86 loc) · 2.67 KB
/
writer.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
package goexcelerate
import (
"archive/zip"
"embed"
"fmt"
"text/template"
"os"
"time"
)
//go:embed all:templates
var templateAssets embed.FS
type Writer struct {
DocPropsAppTemplate *template.Template
DocPropsCoreTemplate *template.Template
ContentTypesTemplate *template.Template
RelTemplate *template.Template
WorkbookTemplate *template.Template
WorkbookRelsTemplate *template.Template
WorksheetTemplate *template.Template
VbaProjectBinFile string
PWorkbook *Workbook
}
func NewWriter() *Writer {
writer := &Writer{
DocPropsAppTemplate: template.Must(template.ParseFS(templateAssets, "templates/docProps/app.xml")),
DocPropsCoreTemplate: template.Must(template.ParseFS(templateAssets, "templates/docProps/core.xml")),
ContentTypesTemplate: template.Must(template.ParseFS(templateAssets, "templates/Content_Types.xml")),
RelTemplate: template.Must(template.ParseFS(templateAssets, "templates/_rels/.rels")),
WorkbookTemplate: template.Must(template.ParseFS(templateAssets, "templates/xl/workbook.xml")),
WorkbookRelsTemplate: template.Must(template.ParseFS(templateAssets, "templates/xl/_rels/workbook.xml.rels")),
WorksheetTemplate: template.Must(template.ParseFS(templateAssets, "templates/xl/worksheets/sheet.xml")),
//VbaProjectBinFile: "",
}
return writer
}
func (wr *Writer) Save(fileHandle string) {
archive, err := os.Create(fileHandle)
if err != nil {
panic(err)
}
defer archive.Close()
zipWriter := zip.NewWriter(archive)
docWr, err := zipWriter.Create("docProps/app.xml")
if err != nil {
panic(err)
}
wr.DocPropsAppTemplate.Execute(docWr, wr.PWorkbook)
coreWr, err := zipWriter.Create("docProps/core.xml")
if err != nil {
panic(err)
}
crDate := struct {
Date string
}{Date: time.Now().Format(time.DateTime)}
wr.DocPropsCoreTemplate.Execute(coreWr, crDate)
contentTypesWr, err := zipWriter.Create("[Content_Types].xml")
if err != nil {
panic(err)
}
wr.ContentTypesTemplate.Execute(contentTypesWr, wr.PWorkbook)
relsWr, err := zipWriter.Create("_rels/.rels")
if err != nil {
panic(err)
}
wr.RelTemplate.Execute(relsWr, true)
wbWr, err := zipWriter.Create("xl/workbook.xml")
if err != nil {
panic(err)
}
wr.WorkbookTemplate.Execute(wbWr, wr.PWorkbook)
wbRelsWr, err := zipWriter.Create("xl/_rels/workbook.xml.rels")
if err != nil {
panic(err)
}
wr.WorkbookRelsTemplate.Execute(wbRelsWr, wr.PWorkbook)
// write all the worksheets
for index, sheet := range wr.PWorkbook.Worksheets {
fname := fmt.Sprintf("xl/worksheets/sheet%d.xml", index)
tWr, err := zipWriter.Create(fname)
if err != nil {
panic(err)
}
wr.WorksheetTemplate.Execute(tWr, sheet)
}
zipWriter.Close()
}