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.Id[3:])
  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. }