file.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. refTable.isWrite = true
  95. var workbookRels WorkBookRels = make(WorkBookRels)
  96. var err error
  97. var workbook xlsxWorkbook
  98. var types xlsxTypes = MakeDefaultContentTypes()
  99. marshal := func(thing interface{}) (string, error) {
  100. body, err := xml.MarshalIndent(thing, " ", " ")
  101. if err != nil {
  102. return "", err
  103. }
  104. return xml.Header + string(body), nil
  105. }
  106. parts = make(map[string]string)
  107. workbook = f.makeWorkbook()
  108. sheetIndex := 1
  109. for _, sheet := range f.Sheets {
  110. xSheet := sheet.makeXLSXSheet(refTable)
  111. rId := fmt.Sprintf("rId%d", sheetIndex)
  112. sheetId := strconv.Itoa(sheetIndex)
  113. sheetPath := fmt.Sprintf("worksheets/sheet%d.xml", sheetIndex)
  114. partName := "xl/" + sheetPath
  115. types.Overrides = append(
  116. types.Overrides,
  117. xlsxOverride{
  118. PartName: partName,
  119. ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"})
  120. workbookRels[rId] = sheetPath
  121. workbook.Sheets.Sheet[sheetIndex-1] = xlsxSheet{
  122. Name: sheet.Name,
  123. SheetId: sheetId,
  124. Id: rId}
  125. parts[partName], err = marshal(xSheet)
  126. if err != nil {
  127. return parts, err
  128. }
  129. sheetIndex++
  130. }
  131. parts["xl/workbook.xml"], err = marshal(workbook)
  132. if err != nil {
  133. return parts, err
  134. }
  135. parts["_rels/.rels"] = `<?xml version="1.0" encoding="UTF-8"?>
  136. <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  137. <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
  138. <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
  139. <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
  140. </Relationships>`
  141. parts["docProps/app.xml"] = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  142. <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
  143. <TotalTime>0</TotalTime>
  144. <Application>Go XLSX</Application>
  145. </Properties>`
  146. // TODO - do this properly, modification and revision information
  147. parts["docProps/core.xml"] = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  148. <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>`
  149. xSST := refTable.makeXLSXSST()
  150. parts["xl/sharedStrings.xml"], err = marshal(xSST)
  151. if err != nil {
  152. return parts, err
  153. }
  154. xWRel := workbookRels.MakeXLSXWorkbookRels()
  155. parts["xl/_rels/workbook.xml.rels"], err = marshal(xWRel)
  156. if err != nil {
  157. return parts, err
  158. }
  159. parts["[Content_Types].xml"], err = marshal(types)
  160. if err != nil {
  161. return parts, err
  162. }
  163. parts["xl/styles.xml"] = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  164. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  165. </styleSheet>`
  166. return parts, nil
  167. }