file.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package xlsx
  2. import (
  3. "archive/zip"
  4. "encoding/xml"
  5. "fmt"
  6. "io"
  7. "os"
  8. "strconv"
  9. )
  10. // File is a high level structure providing a slice of Sheet structs
  11. // to the user.
  12. type File struct {
  13. worksheets map[string]*zip.File
  14. numFmtRefTable map[int]xlsxNumFmt
  15. referenceTable *RefTable
  16. Date1904 bool
  17. styles *xlsxStyles
  18. Sheets []*Sheet
  19. Sheet map[string]*Sheet
  20. }
  21. // Create a new File
  22. func NewFile() (file *File) {
  23. file = &File{};
  24. file.Sheet = make(map[string]*Sheet)
  25. file.Sheets = make([]*Sheet, 0)
  26. return
  27. }
  28. // OpenFile() take the name of an XLSX file and returns a populated
  29. // xlsx.File struct for it.
  30. func OpenFile(filename string) (*File, error) {
  31. var f *zip.ReadCloser
  32. f, err := zip.OpenReader(filename)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return ReadZip(f)
  37. }
  38. // Save the File to an xlsx file at the provided path.
  39. func (f *File) Save(path string) (err error) {
  40. var parts map[string]string
  41. var target *os.File
  42. var zipWriter *zip.Writer
  43. parts, err = f.MarshallParts()
  44. if err != nil {
  45. return
  46. }
  47. target, err = os.Create(path)
  48. if err != nil {
  49. return
  50. }
  51. zipWriter = zip.NewWriter(target)
  52. for partName, part := range parts {
  53. var writer io.Writer
  54. writer, err = zipWriter.Create(partName)
  55. if err != nil {
  56. return
  57. }
  58. _, err = writer.Write([]byte(part))
  59. if err != nil {
  60. return
  61. }
  62. }
  63. err = zipWriter.Close()
  64. if err != nil {
  65. return
  66. }
  67. return target.Close()
  68. }
  69. // Add a new Sheet, with the provided name, to a File
  70. func (f *File) AddSheet(sheetName string) (sheet *Sheet) {
  71. sheet = &Sheet{Name: sheetName}
  72. f.Sheet[sheetName] = sheet
  73. f.Sheets = append(f.Sheets, sheet)
  74. return sheet
  75. }
  76. func (f *File) makeWorkbook() xlsxWorkbook {
  77. var workbook xlsxWorkbook
  78. workbook = xlsxWorkbook{}
  79. workbook.FileVersion = xlsxFileVersion{}
  80. workbook.FileVersion.AppName = "Go XLSX"
  81. workbook.WorkbookPr = xlsxWorkbookPr{BackupFile: false}
  82. workbook.BookViews = xlsxBookViews{}
  83. workbook.BookViews.WorkBookView = make([]xlsxWorkBookView, 1)
  84. workbook.BookViews.WorkBookView[0] = xlsxWorkBookView{}
  85. workbook.Sheets = xlsxSheets{}
  86. workbook.Sheets.Sheet = make([]xlsxSheet, len(f.Sheets))
  87. return workbook
  88. }
  89. // Construct a map of file name to XML content representing the file
  90. // in terms of the structure of an XLSX file.
  91. func (f *File) MarshallParts() (map[string]string, error) {
  92. var parts map[string]string
  93. var refTable *RefTable = NewSharedStringRefTable()
  94. var workbookRels WorkBookRels = make(WorkBookRels)
  95. var err error
  96. var workbook xlsxWorkbook
  97. var types xlsxTypes = MakeDefaultContentTypes()
  98. marshal := func(thing interface{}) (string, error) {
  99. body, err := xml.MarshalIndent(thing, " ", " ")
  100. if err != nil {
  101. return "", err
  102. }
  103. return xml.Header + string(body), nil
  104. }
  105. parts = make(map[string]string)
  106. workbook = f.makeWorkbook()
  107. sheetIndex := 1
  108. for _, sheet := range f.Sheets {
  109. xSheet := sheet.makeXLSXSheet(refTable)
  110. rId := fmt.Sprintf("rId%d", sheetIndex)
  111. sheetId := strconv.Itoa(sheetIndex)
  112. sheetPath := fmt.Sprintf("worksheets/sheet%d.xml", sheetIndex)
  113. partName := "xl/" + sheetPath
  114. types.Overrides = append(
  115. types.Overrides,
  116. xlsxOverride{
  117. PartName: partName,
  118. ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"})
  119. workbookRels[rId] = sheetPath
  120. workbook.Sheets.Sheet[sheetIndex - 1] = xlsxSheet{
  121. Name: sheet.Name,
  122. SheetId: sheetId,
  123. Id: rId}
  124. parts[partName], err = marshal(xSheet)
  125. if err != nil {
  126. return parts, err
  127. }
  128. sheetIndex++
  129. }
  130. parts["xl/workbook.xml"], err = marshal(workbook)
  131. if err != nil {
  132. return parts, err
  133. }
  134. parts["_rels/.rels"] = `<?xml version="1.0" encoding="UTF-8"?>
  135. <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  136. <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
  137. <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
  138. <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
  139. </Relationships>`
  140. parts["docProps/app.xml"] = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  141. <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
  142. <TotalTime>0</TotalTime>
  143. <Application>Go XLSX</Application>
  144. </Properties>`
  145. // TODO - do this properly, modification and revision information
  146. parts["docProps/core.xml"] = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  147. <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></cp:coreProperties>`
  148. xSST := refTable.makeXLSXSST()
  149. parts["xl/sharedStrings.xml"], err = marshal(xSST)
  150. if err != nil {
  151. return parts, err
  152. }
  153. xWRel := workbookRels.MakeXLSXWorkbookRels()
  154. parts["xl/_rels/workbook.xml.rels"], err = marshal(xWRel)
  155. if err != nil {
  156. return parts, err
  157. }
  158. parts["[Content_Types].xml"], err = marshal(types)
  159. if err != nil {
  160. return parts, err
  161. }
  162. parts["xl/styles.xml"] = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  163. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  164. </styleSheet>`
  165. return parts, nil
  166. }