workbook.go 5.0 KB

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