workbook.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }
  47. // xlsxBookViews directly maps the bookViews element from the
  48. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  49. // - currently I have not checked it for completeness - it does as
  50. // much as I need.
  51. type xlsxBookViews struct {
  52. WorkBookView []xlsxWorkBookView `xml:"workbookView"`
  53. }
  54. // xlsxWorkBookView directly maps the workbookView element from the
  55. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  56. // - currently I have not checked it for completeness - it does as
  57. // much as I need.
  58. type xlsxWorkBookView struct {
  59. XWindow string `xml:"xWindow,attr"`
  60. YWindow string `xml:"yWindow,attr"`
  61. WindowWidth string `xml:"windowWidth,attr"`
  62. WindowHeight string `xml:"windowHeight,attr"`
  63. }
  64. // xlsxSheets directly maps the sheets element from the namespace
  65. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  66. // currently I have not checked it for completeness - it does as much
  67. // as I need.
  68. type xlsxSheets struct {
  69. Sheet []xlsxSheet `xml:"sheet"`
  70. }
  71. // xlsxSheet directly maps the sheet element from the namespace
  72. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  73. // currently I have not checked it for completeness - it does as much
  74. // as I need.
  75. type xlsxSheet struct {
  76. Name string `xml:"name,attr"`
  77. SheetId string `xml:"sheetId,attr"`
  78. Id string `xml:"id,attr"`
  79. }
  80. // xlsxDefinedNames directly maps the definedNames element from the
  81. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  82. // - currently I have not checked it for completeness - it does as
  83. // much as I need.
  84. type xlsxDefinedNames struct {
  85. DefinedName []xlsxDefinedName `xml:"definedName"`
  86. }
  87. // xlsxDefinedName directly maps the definedName element from the
  88. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  89. // - currently I have not checked it for completeness - it does as
  90. // much as I need.
  91. type xlsxDefinedName struct {
  92. Data string `xml:",chardata"`
  93. Name string `xml:"name,attr"`
  94. LocalSheetID string `xml:"localSheetId,attr"`
  95. }
  96. // xlsxCalcPr directly maps the calcPr element from the namespace
  97. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  98. // currently I have not checked it for completeness - it does as much
  99. // as I need.
  100. type xlsxCalcPr struct {
  101. CalcId string `xml:"calcId,attr"`
  102. }
  103. // getWorksheetFromSheet() is an internal helper function to open a
  104. // sheetN.xml file, refered to by an xlsx.xlsxSheet struct, from the XLSX
  105. // file and unmarshal it an xlsx.xlsxWorksheet struct
  106. func getWorksheetFromSheet(sheet xlsxSheet, worksheets map[string]*zip.File, sheetXMLMap map[string]string) (*xlsxWorksheet, error) {
  107. var rc io.ReadCloser
  108. var decoder *xml.Decoder
  109. var worksheet *xlsxWorksheet
  110. var error error
  111. var sheetName string
  112. worksheet = new(xlsxWorksheet)
  113. sheetName, ok := sheetXMLMap[sheet.Id]
  114. if !ok {
  115. if sheet.SheetId != "" {
  116. sheetName = fmt.Sprintf("sheet%s", sheet.SheetId)
  117. } else {
  118. sheetName = fmt.Sprintf("sheet%s", sheet.Id)
  119. }
  120. }
  121. f := worksheets[sheetName]
  122. rc, error = f.Open()
  123. if error != nil {
  124. return nil, error
  125. }
  126. decoder = xml.NewDecoder(rc)
  127. error = decoder.Decode(worksheet)
  128. if error != nil {
  129. return nil, error
  130. }
  131. return worksheet, nil
  132. }