workbook.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package xlsx
  2. import (
  3. "archive/zip"
  4. "encoding/xml"
  5. "fmt"
  6. "io"
  7. )
  8. // XLSXWorkbook directly maps the workbook element from the namespace
  9. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  10. // currently I have not checked it for completeness - it does as much
  11. // as I need.
  12. type XLSXWorkbook struct {
  13. FileVersion XLSXFileVersion `xml:"fileVersion"`
  14. WorkbookPr XLSXWorkbookPr `xml:"workbookPr"`
  15. BookViews XLSXBookViews `xml:"bookViews"`
  16. Sheets XLSXSheets `xml:"sheets"`
  17. DefinedNames XLSXDefinedNames `xml:"definedNames"`
  18. CalcPr XLSXCalcPr `xml:"calcPr"`
  19. }
  20. // XLSXFileVersion directly maps the fileVersion element from the
  21. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  22. // - currently I have not checked it for completeness - it does as
  23. // much as I need.
  24. type XLSXFileVersion struct {
  25. AppName string `xml:"appName,attr"`
  26. LastEdited string `xml:"lastEdited,attr"`
  27. LowestEdited string `xml:"lowestEdited,attr"`
  28. RupBuild string `xml:"rupBuild,attr"`
  29. }
  30. // XLSXWorkbookPr directly maps the workbookPr 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 XLSXWorkbookPr struct {
  35. DefaultThemeVersion string `xml:"defaultThemeVersion,attr"`
  36. }
  37. // XLSXBookViews directly maps the bookViews element from the
  38. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  39. // - currently I have not checked it for completeness - it does as
  40. // much as I need.
  41. type XLSXBookViews struct {
  42. WorkBookView []XLSXWorkBookView `xml:"workbookView"`
  43. }
  44. // XLSXWorkBookView directly maps the workbookView element from the
  45. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  46. // - currently I have not checked it for completeness - it does as
  47. // much as I need.
  48. type XLSXWorkBookView struct {
  49. XWindow string `xml:"xWindow,attr"`
  50. YWindow string `xml:"yWindow,attr"`
  51. WindowWidth string `xml:"windowWidth,attr"`
  52. WindowHeight string `xml:"windowHeight,attr"`
  53. }
  54. // XLSXSheets directly maps the sheets element from the namespace
  55. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  56. // currently I have not checked it for completeness - it does as much
  57. // as I need.
  58. type XLSXSheets struct {
  59. Sheet []XLSXSheet `xml:"sheet"`
  60. }
  61. // XLSXSheet directly maps the sheet element from the namespace
  62. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  63. // currently I have not checked it for completeness - it does as much
  64. // as I need.
  65. type XLSXSheet struct {
  66. Name string `xml:"name,attr"`
  67. SheetId string `xml:"sheetId,attr"`
  68. Id string `xml:"id,attr"`
  69. }
  70. // XLSXDefinedNames directly maps the definedNames element from the
  71. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  72. // - currently I have not checked it for completeness - it does as
  73. // much as I need.
  74. type XLSXDefinedNames struct {
  75. DefinedName []XLSXDefinedName `xml:"definedName"`
  76. }
  77. // XLSXDefinedName directly maps the definedName element from the
  78. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  79. // - currently I have not checked it for completeness - it does as
  80. // much as I need.
  81. type XLSXDefinedName struct {
  82. Data string `xml:",chardata"`
  83. Name string `xml:"name,attr"`
  84. LocalSheetID string `xml:"localSheetId,attr"`
  85. }
  86. // XLSXCalcPr directly maps the calcPr element from the namespace
  87. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  88. // currently I have not checked it for completeness - it does as much
  89. // as I need.
  90. type XLSXCalcPr struct {
  91. CalcId string `xml:"calcId,attr"`
  92. }
  93. // getWorksheetFromSheet() is an internal helper function to open a sheetN.xml file, refered to by an xlsx.XLSXSheet struct, from the XLSX file and unmarshal it an xlsx.XLSXWorksheet struct
  94. func getWorksheetFromSheet(sheet XLSXSheet, worksheets map[string]*zip.File) (*XLSXWorksheet, error) {
  95. var rc io.ReadCloser
  96. var decoder *xml.Decoder
  97. var worksheet *XLSXWorksheet
  98. var error error
  99. worksheet = new(XLSXWorksheet)
  100. sheetName := fmt.Sprintf("sheet%s", sheet.SheetId)
  101. f := worksheets[sheetName]
  102. rc, error = f.Open()
  103. if error != nil {
  104. return nil, error
  105. }
  106. decoder = xml.NewDecoder(rc)
  107. error = decoder.Decode(worksheet)
  108. if error != nil {
  109. return nil, error
  110. }
  111. return worksheet, nil
  112. }