-
Notifications
You must be signed in to change notification settings - Fork 1
/
relations.go
63 lines (56 loc) · 1.64 KB
/
relations.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
package streamxlsx
import (
"encoding/xml"
"fmt"
"io"
)
type relationshipsXML struct {
XMLName string `xml:"Relationships"`
XMLNS string `xml:"xmlns,attr"`
Relationships []relationship `xml:"Relationship"`
}
type relationship struct {
ID string `xml:"Id,attr"`
Target string `xml:"Target,attr"`
Type string `xml:"Type,attr"`
TargetMode string `xml:"TargetMode,attr,omitempty"`
}
func writeRelations(fh io.Writer) error {
return writeRelations_(fh, []relationship{
{
ID: "rId1",
Target: "/xl/workbook.xml",
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
},
})
}
func writeWorkbookRelations(fh io.Writer, sheetTitles []string) error {
var rels = []relationship{
{
ID: "style1",
Target: "/xl/styles.xml",
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
},
{
ID: "shared1",
Target: "/xl/sharedStrings.xml",
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
},
}
for i := range sheetTitles {
rels = append(rels, relationship{
ID: fmt.Sprintf("sheetId%d", i+1),
Target: fmt.Sprintf("/xl/worksheets/sheet%d.xml", i+1),
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
})
}
return writeRelations_(fh, rels)
}
func writeRelations_(fh io.Writer, rels []relationship) error {
fh.Write([]byte(xml.Header))
enc := xml.NewEncoder(fh)
return enc.Encode(relationshipsXML{
XMLNS: "http://schemas.openxmlformats.org/package/2006/relationships",
Relationships: rels,
})
}