workbook.go 5.0 KB

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