-
Notifications
You must be signed in to change notification settings - Fork 1
/
workbook.go
40 lines (34 loc) · 893 Bytes
/
workbook.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
package streamxlsx
import (
"encoding/xml"
"fmt"
"io"
)
type workbookXML struct {
XMLName string `xml:"workbook"`
XMLNS string `xml:"xmlns,attr"`
XMLNSR string `xml:"xmlns:r,attr"`
Sheets []sheetXML `xml:"sheets>sheet"`
}
type sheetXML struct {
Name string `xml:"name,attr"`
ID string `xml:"sheetId,attr"`
RID string `xml:"r:id,attr"`
}
func writeWorkbook(fh io.Writer, sheetTitles []string) error {
fh.Write([]byte(xml.Header))
enc := xml.NewEncoder(fh)
var sheets []sheetXML
for i, title := range sheetTitles {
sheets = append(sheets, sheetXML{
Name: title,
ID: fmt.Sprintf("%d", i+1),
RID: fmt.Sprintf("sheetId%d", i+1),
})
}
return enc.Encode(workbookXML{
XMLNS: "http://schemas.openxmlformats.org/spreadsheetml/2006/main",
XMLNSR: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
Sheets: sheets,
})
}