xmlWorkbook.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package xlsx
  2. import (
  3. "archive/zip"
  4. "encoding/xml"
  5. "fmt"
  6. "io"
  7. )
  8. // xmlxWorkbookRels contains xmlxWorkbookRelations
  9. // which maps sheet id and sheet XML
  10. type xlsxWorkbookRels struct {
  11. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/relationships Relationships"`
  12. Relationships []xlsxWorkbookRelation `xml:"Relationship"`
  13. }
  14. // xmlxWorkbookRelation maps sheet id and xl/worksheets/sheet%d.xml
  15. type xlsxWorkbookRelation struct {
  16. Id string `xml:",attr"`
  17. Target string `xml:",attr"`
  18. Type string `xml:",attr"`
  19. }
  20. // xlsxWorkbook directly maps the workbook element from the namespace
  21. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  22. // currently I have not checked it for completeness - it does as much
  23. // as I need.
  24. type xlsxWorkbook struct {
  25. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main workbook"`
  26. FileVersion xlsxFileVersion `xml:"fileVersion"`
  27. WorkbookPr xlsxWorkbookPr `xml:"workbookPr"`
  28. BookViews xlsxBookViews `xml:"bookViews"`
  29. Sheets xlsxSheets `xml:"sheets"`
  30. DefinedNames xlsxDefinedNames `xml:"definedNames"`
  31. CalcPr xlsxCalcPr `xml:"calcPr"`
  32. }
  33. // xlsxFileVersion directly maps the fileVersion element from the
  34. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  35. // - currently I have not checked it for completeness - it does as
  36. // much as I need.
  37. type xlsxFileVersion struct {
  38. AppName string `xml:"appName,attr,omitempty"`
  39. LastEdited string `xml:"lastEdited,attr,omitempty"`
  40. LowestEdited string `xml:"lowestEdited,attr,omitempty"`
  41. RupBuild string `xml:"rupBuild,attr,omitempty"`
  42. }
  43. // xlsxWorkbookPr directly maps the workbookPr element from the
  44. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  45. // - currently I have not checked it for completeness - it does as
  46. // much as I need.
  47. type xlsxWorkbookPr struct {
  48. DefaultThemeVersion string `xml:"defaultThemeVersion,attr,omitempty"`
  49. BackupFile bool `xml:"backupFile,attr,omitempty"`
  50. ShowObjects string `xml:"showObjects,attr,omitempty"`
  51. Date1904 bool `xml:"date1904,attr"`
  52. }
  53. // xlsxBookViews directly maps the bookViews element from the
  54. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  55. // - currently I have not checked it for completeness - it does as
  56. // much as I need.
  57. type xlsxBookViews struct {
  58. WorkBookView []xlsxWorkBookView `xml:"workbookView"`
  59. }
  60. // xlsxWorkBookView directly maps the workbookView element from the
  61. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  62. // - currently I have not checked it for completeness - it does as
  63. // much as I need.
  64. type xlsxWorkBookView struct {
  65. ActiveTab int `xml:"activeTab,attr,omitempty"`
  66. FirstSheet int `xml:"firstSheet,attr,omitempty"`
  67. ShowHorizontalScroll bool `xml:"showHorizontalScroll,attr,omitempty"`
  68. ShowVerticalScroll bool `xml:"showVerticalScroll,attr,omitempty"`
  69. ShowSheetTabs bool `xml:"showSheetTabs,attr,omitempty"`
  70. TabRatio int `xml:"tabRatio,attr,omitempty"`
  71. WindowHeight int `xml:"windowHeight,attr,omitempty"`
  72. WindowWidth int `xml:"windowWidth,attr,omitempty"`
  73. XWindow string `xml:"xWindow,attr,omitempty"`
  74. YWindow string `xml:"yWindow,attr,omitempty"`
  75. }
  76. // xlsxSheets directly maps the sheets element from the namespace
  77. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  78. // currently I have not checked it for completeness - it does as much
  79. // as I need.
  80. type xlsxSheets struct {
  81. Sheet []xlsxSheet `xml:"sheet"`
  82. }
  83. // xlsxSheet directly maps the sheet element from the namespace
  84. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  85. // currently I have not checked it for completeness - it does as much
  86. // as I need.
  87. type xlsxSheet struct {
  88. Name string `xml:"name,attr,omitempty"`
  89. SheetId string `xml:"sheetId,attr,omitempty"`
  90. Id string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
  91. }
  92. // xlsxDefinedNames directly maps the definedNames element from the
  93. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  94. // - currently I have not checked it for completeness - it does as
  95. // much as I need.
  96. type xlsxDefinedNames struct {
  97. DefinedName []xlsxDefinedName `xml:"definedName"`
  98. }
  99. // xlsxDefinedName directly maps the definedName element from the
  100. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  101. // - currently I have not checked it for completeness - it does as
  102. // much as I need.
  103. type xlsxDefinedName struct {
  104. Data string `xml:",chardata"`
  105. Name string `xml:"name,attr"`
  106. LocalSheetID string `xml:"localSheetId,attr"`
  107. }
  108. // xlsxCalcPr directly maps the calcPr element from the namespace
  109. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  110. // currently I have not checked it for completeness - it does as much
  111. // as I need.
  112. type xlsxCalcPr struct {
  113. CalcId string `xml:"calcId,attr,omitempty"`
  114. IterateCount int `xml:"iterateCount,attr,omitempty"`
  115. RefMode string `xml:"refMode,attr,omitempty"`
  116. Iterate bool `xml:"iterate,attr,omitempty"`
  117. IterateDelta float64 `xml:"iterateDelta,attr,omitempty"`
  118. }
  119. // getWorksheetFromSheet() is an internal helper function to open a
  120. // sheetN.xml file, refered to by an xlsx.xlsxSheet struct, from the XLSX
  121. // file and unmarshal it an xlsx.xlsxWorksheet struct
  122. func getWorksheetFromSheet(sheet xlsxSheet, worksheets map[string]*zip.File, sheetXMLMap map[string]string) (*xlsxWorksheet, error) {
  123. var rc io.ReadCloser
  124. var decoder *xml.Decoder
  125. var worksheet *xlsxWorksheet
  126. var error error
  127. var sheetName string
  128. worksheet = new(xlsxWorksheet)
  129. sheetName, ok := sheetXMLMap[sheet.Id]
  130. if !ok {
  131. if sheet.SheetId != "" {
  132. sheetName = fmt.Sprintf("sheet%s", sheet.SheetId)
  133. } else {
  134. sheetName = fmt.Sprintf("sheet%s", sheet.Id)
  135. }
  136. }
  137. f := worksheets[sheetName]
  138. rc, error = f.Open()
  139. if error != nil {
  140. return nil, error
  141. }
  142. decoder = xml.NewDecoder(rc)
  143. error = decoder.Decode(worksheet)
  144. if error != nil {
  145. return nil, error
  146. }
  147. return worksheet, nil
  148. }