forked from qax-os/excelize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
87 lines (80 loc) · 1.93 KB
/
file.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
package excelize
import (
"archive/zip"
"bytes"
"encoding/xml"
"fmt"
"io"
"os"
)
// CreateFile provides function to create new file by default template. For
// example:
//
// xlsx := CreateFile()
//
func CreateFile() *File {
file := make(map[string]string)
file["_rels/.rels"] = templateRels
file["docProps/app.xml"] = templateDocpropsApp
file["docProps/core.xml"] = templateDocpropsCore
file["xl/_rels/workbook.xml.rels"] = templateWorkbookRels
file["xl/theme/theme1.xml"] = templateTheme
file["xl/worksheets/sheet1.xml"] = templateSheet
file["xl/styles.xml"] = templateStyles
file["xl/workbook.xml"] = templateWorkbook
file["[Content_Types].xml"] = templateContentTypes
return &File{
XLSX: file,
Sheet: make(map[string]*xlsxWorksheet),
}
}
// Save provides function to override the xlsx file with origin path.
func (f *File) Save() error {
if f.Path == "" {
return fmt.Errorf("No path defined for file, consider File.WriteTo or File.Write")
}
return f.WriteTo(f.Path)
}
// WriteTo provides function to create or update to an xlsx file at the provided
// path.
func (f *File) WriteTo(name string) error {
file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return err
}
defer file.Close()
return f.Write(file)
}
// Write provides function to write to an io.Writer.
func (f *File) Write(w io.Writer) error {
buf := new(bytes.Buffer)
zw := zip.NewWriter(buf)
for path, sheet := range f.Sheet {
if sheet == nil {
continue
}
output, err := xml.Marshal(sheet)
if err != nil {
return err
}
f.saveFileList(path, replaceWorkSheetsRelationshipsNameSpace(string(output)))
}
for path, content := range f.XLSX {
fi, err := zw.Create(path)
if err != nil {
return err
}
_, err = fi.Write([]byte(content))
if err != nil {
return err
}
}
err := zw.Close()
if err != nil {
return err
}
if _, err := buf.WriteTo(w); err != nil {
return err
}
return nil
}