workbook.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package xlsx
  2. import (
  3. "archive/zip"
  4. "fmt"
  5. "io"
  6. "os"
  7. "xml"
  8. )
  9. // XLSXWorkbook directly maps the workbook element from the namespace
  10. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  11. // currently I have not checked it for completeness - it does as much
  12. // as I need.
  13. type XLSXWorkbook struct {
  14. FileVersion XLSXFileVersion
  15. WorkbookPr XLSXWorkbookPr
  16. BookViews XLSXBookViews
  17. Sheets XLSXSheets
  18. DefinedNames XLSXDefinedNames
  19. CalcPr XLSXCalcPr
  20. }
  21. // XLSXFileVersion directly maps the fileVersion element from the
  22. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  23. // - currently I have not checked it for completeness - it does as
  24. // much as I need.
  25. type XLSXFileVersion struct {
  26. AppName string `xml:"attr"`
  27. LastEdited string `xml:"attr"`
  28. LowestEdited string `xml:"attr"`
  29. RupBuild string `xml:"attr"`
  30. }
  31. // XLSXWorkbookPr directly maps the workbookPr 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 XLSXWorkbookPr struct {
  36. DefaultThemeVersion string `xml:"attr"`
  37. }
  38. // XLSXBookViews directly maps the bookViews element from the
  39. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  40. // - currently I have not checked it for completeness - it does as
  41. // much as I need.
  42. type XLSXBookViews struct {
  43. WorkBookView []XLSXWorkBookView
  44. }
  45. // XLSXWorkBookView directly maps the workbookView element from the
  46. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  47. // - currently I have not checked it for completeness - it does as
  48. // much as I need.
  49. type XLSXWorkBookView struct {
  50. XWindow string `xml:"attr"`
  51. YWindow string `xml:"attr"`
  52. WindowWidth string `xml:"attr"`
  53. WindowHeight string `xml:"attr"`
  54. }
  55. // XLSXSheets directly maps the sheets element from the namespace
  56. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  57. // currently I have not checked it for completeness - it does as much
  58. // as I need.
  59. type XLSXSheets struct {
  60. Sheet []XLSXSheet
  61. }
  62. // XLSXSheet directly maps the sheet element from the namespace
  63. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  64. // currently I have not checked it for completeness - it does as much
  65. // as I need.
  66. type XLSXSheet struct {
  67. Name string `xml:"attr"`
  68. SheetId string `xml:"attr"`
  69. Id string `xml:"attr"`
  70. }
  71. // XLSXDefinedNames directly maps the definedNames element from the
  72. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  73. // - currently I have not checked it for completeness - it does as
  74. // much as I need.
  75. type XLSXDefinedNames struct {
  76. DefinedName []XLSXDefinedName
  77. }
  78. // XLSXDefinedName directly maps the definedName element from the
  79. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  80. // - currently I have not checked it for completeness - it does as
  81. // much as I need.
  82. type XLSXDefinedName struct {
  83. Data string `xml:"chardata"`
  84. Name string `xml:"attr"`
  85. LocalSheetID string `xml:"attr"`
  86. }
  87. // XLSXCalcPr directly maps the calcPr element from the namespace
  88. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  89. // currently I have not checked it for completeness - it does as much
  90. // as I need.
  91. type XLSXCalcPr struct {
  92. CalcId string `xml:"attr"`
  93. }
  94. // 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
  95. func getWorksheetFromSheet(sheet XLSXSheet, worksheets map[string]*zip.File) (*XLSXWorksheet, os.Error) {
  96. var rc io.ReadCloser
  97. var worksheet *XLSXWorksheet
  98. var error os.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. error = xml.Unmarshal(rc, worksheet)
  107. if error != nil {
  108. return nil, error
  109. }
  110. return worksheet, nil
  111. }