xmlWorksheet.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package xlsx
  2. import (
  3. "encoding/xml"
  4. )
  5. // xlsxWorksheet directly maps the worksheet element in the namespace
  6. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  7. // currently I have not checked it for completeness - it does as much
  8. // as I need.
  9. type xlsxWorksheet struct {
  10. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"`
  11. SheetPr xlsxSheetPr `xml:"sheetPr"`
  12. Dimension xlsxDimension `xml:"dimension"`
  13. SheetViews xlsxSheetViews `xml:"sheetViews"`
  14. SheetFormatPr xlsxSheetFormatPr `xml:"sheetFormatPr"`
  15. Cols xlsxCols `xml:"cols"`
  16. SheetData xlsxSheetData `xml:"sheetData"`
  17. PrintOptions xlsxPrintOptions `xml:"printOptions"`
  18. PageMargins xlsxPageMargins `xml:"pageMargins"`
  19. PageSetUp xlsxPageSetUp `xml:"pageSetup"`
  20. HeaderFooter xlsxHeaderFooter `xml:"headerFooter"`
  21. }
  22. // xlsxHeaderFooter directly maps the headerFooter element in the namespace
  23. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  24. // currently I have not checked it for completeness - it does as much
  25. // as I need.
  26. type xlsxHeaderFooter struct {
  27. DifferentFirst bool `xml:"differentFirst,attr"`
  28. DifferentOddEven bool `xml:"differentOddEven,attr"`
  29. OddHeader []xlsxOddHeader `xml:"oddHeader"`
  30. OddFooter []xlsxOddFooter `xml:"oddFooter"`
  31. }
  32. // xlsxOddHeader directly maps the oddHeader element in the namespace
  33. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  34. // currently I have not checked it for completeness - it does as much
  35. // as I need.
  36. type xlsxOddHeader struct {
  37. Content string `xml:",chardata"`
  38. }
  39. // xlsxOddFooter directly maps the oddFooter element in the namespace
  40. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  41. // currently I have not checked it for completeness - it does as much
  42. // as I need.
  43. type xlsxOddFooter struct {
  44. Content string `xml:",chardata"`
  45. }
  46. // xlsxPageSetUp directly maps the pageSetup element in the namespace
  47. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  48. // currently I have not checked it for completeness - it does as much
  49. // as I need.
  50. type xlsxPageSetUp struct {
  51. PaperSize string `xml:"paperSize,attr"`
  52. Scale int `xml:"scale,attr"`
  53. FirstPageNumber int `xml:"firstPageNumber,attr"`
  54. FitToWidth int `xml:"fitToWidth,attr"`
  55. FitToHeight int `xml:"fitToHeight,attr"`
  56. PageOrder string `xml:"pageOrder,attr"`
  57. Orientation string `xml:"orientation,attr"`
  58. UsePrinterDefaults bool `xml:"usePrinterDefaults,attr"`
  59. BlackAndWhite bool `xml:"blackAndWhite,attr"`
  60. Draft bool `xml:"draft,attr"`
  61. CellComments string `xml:"cellComments,attr"`
  62. UseFirstPageNumber bool `xml:"useFirstPageNumber,attr"`
  63. HorizontalDPI float32 `xml:"horizontalDpi,attr"`
  64. VerticalDPI float32 `xml:"verticalDpi,attr"`
  65. Copies int `xml:"copies,attr"`
  66. }
  67. // xlsxPrintOptions directly maps the printOptions element in 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 xlsxPrintOptions struct {
  72. Headings bool `xml:"headings,attr"`
  73. GridLines bool `xml:"gridLines,attr"`
  74. GridLinesSet bool `xml:"gridLinesSet,attr"`
  75. HorizontalCentered bool `xml:"horizontalCentered,attr"`
  76. VerticalCentered bool `xml:"verticalCentered,attr"`
  77. }
  78. // xlsxPageMargins directly maps the pageMargins element in the namespace
  79. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  80. // currently I have not checked it for completeness - it does as much
  81. // as I need.
  82. type xlsxPageMargins struct {
  83. Left float64 `xml:"left,attr"`
  84. Right float64 `xml:"right,attr"`
  85. Top float64 `xml:"top,attr"`
  86. Bottom float64 `xml:"bottom,attr"`
  87. Header float64 `xml:"header,attr"`
  88. Footer float64 `xml:"footer,attr"`
  89. }
  90. // xlsxSheetFormatPr directly maps the sheetFormatPr element in the namespace
  91. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  92. // currently I have not checked it for completeness - it does as much
  93. // as I need.
  94. type xlsxSheetFormatPr struct {
  95. DefaultColWidth float64 `xml:"defaultColWidth,attr"`
  96. DefaultRowHeight float64 `xml:"defaultRowHeight,attr"`
  97. }
  98. // xlsxSheetViews directly maps the sheetViews element in the namespace
  99. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  100. // currently I have not checked it for completeness - it does as much
  101. // as I need.
  102. type xlsxSheetViews struct {
  103. SheetView []xlsxSheetView `xml:"sheetView"`
  104. }
  105. // xlsxSheetView directly maps the sheetView element in the namespace
  106. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  107. // currently I have not checked it for completeness - it does as much
  108. // as I need.
  109. type xlsxSheetView struct {
  110. WindowProtection bool `xml:"windowProtection,attr"`
  111. ShowFormulas bool `xml:"showFormulas,attr"`
  112. ShowGridLines bool `xml:"showGridLines,attr"`
  113. ShowRowColHeaders bool `xml:"showRowColHeaders,attr"`
  114. ShowZeros bool `xml:"showZeros,attr"`
  115. RightToLeft bool `xml:"rightToLeft,attr"`
  116. TabSelected bool `xml:"tabSelected,attr"`
  117. ShowOutlineSymbols bool `xml:"showOutlineSymbols,attr"`
  118. DefaultGridColor bool `xml:"defaultGridColor,attr"`
  119. View string `xml:"view,attr"`
  120. TopLeftCell string `xml:"topLeftCell,attr"`
  121. ColorId int `xml:"colorId,attr"`
  122. ZoomScale float64 `xml:"zoomScale,attr"`
  123. ZoomScaleNormal float64 `xml:"zoomScaleNormal,attr"`
  124. ZoomScalePageLayoutView float64 `xml:"zoomScalePageLayoutView,attr"`
  125. WorkbookViewId int `xml:"workbookViewId,attr"`
  126. Selection []xlsxSelection `xml:"selection"`
  127. Pane *xlsxPane `xml:"pane"`
  128. }
  129. // xlsxSelection directly maps the selection element in the namespace
  130. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  131. // currently I have not checked it for completeness - it does as much
  132. // as I need.
  133. type xlsxSelection struct {
  134. Pane string `xml:"pane,attr"`
  135. ActiveCell string `xml:"activeCell,attr"`
  136. ActiveCellId int `xml:"activeCellId,attr"`
  137. SQRef string `xml:"sqref,attr"`
  138. }
  139. // xlsxSelection directly maps the selection element in the namespace
  140. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  141. // currently I have not checked it for completeness - it does as much
  142. // as I need.
  143. type xlsxPane struct {
  144. XSplit float64 `xml:"xSplit,attr"`
  145. YSplit float64 `xml:"ySplit,attr"`
  146. TopLeftCell string `xml:"topLeftCell,attr"`
  147. ActivePane string `xml:"activePane,attr"`
  148. State string `xml:"state,attr"` // Either "split" or "frozen"
  149. }
  150. // xlsxSheetPr directly maps the sheetPr element in the namespace
  151. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  152. // currently I have not checked it for completeness - it does as much
  153. // as I need.
  154. type xlsxSheetPr struct {
  155. FilterMode bool `xml:"filterMode,attr"`
  156. PageSetUpPr []xlsxPageSetUpPr `xml:"pageSetUpPr"`
  157. }
  158. // xlsxPageSetUpPr directly maps the pageSetupPr element in the namespace
  159. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  160. // currently I have not checked it for completeness - it does as much
  161. // as I need.
  162. type xlsxPageSetUpPr struct {
  163. FitToPage bool `xml:"fitToPage,attr"`
  164. }
  165. // xlsxCols directly maps the cols element in the namespace
  166. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  167. // currently I have not checked it for completeness - it does as much
  168. // as I need.
  169. type xlsxCols struct {
  170. Col []xlsxCol `xml:"col"`
  171. }
  172. // xlsxCol directly maps the col element in the namespace
  173. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  174. // currently I have not checked it for completeness - it does as much
  175. // as I need.
  176. type xlsxCol struct {
  177. Collapsed bool `xml:"collapsed,attr"`
  178. Hidden bool `xml:"hidden,attr"`
  179. Max int `xml:"max,attr"`
  180. Min int `xml:"min,attr"`
  181. // Style int `xml:"style,attr"`
  182. Width float64 `xml:"width,attr"`
  183. }
  184. // xlsxDimension directly maps the dimension element in the namespace
  185. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  186. // currently I have not checked it for completeness - it does as much
  187. // as I need.
  188. type xlsxDimension struct {
  189. Ref string `xml:"ref,attr"`
  190. }
  191. // xlsxSheetData directly maps the sheetData element in the namespace
  192. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  193. // currently I have not checked it for completeness - it does as much
  194. // as I need.
  195. type xlsxSheetData struct {
  196. XMLName xml.Name `xml:"sheetData"`
  197. Row []xlsxRow `xml:"row"`
  198. }
  199. // xlsxRow directly maps the row element in the namespace
  200. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  201. // currently I have not checked it for completeness - it does as much
  202. // as I need.
  203. type xlsxRow struct {
  204. R int `xml:"r,attr"`
  205. Spans string `xml:"spans,attr,omitempty"`
  206. Hidden bool `xml:"hidden,attr,omitempty"`
  207. C []xlsxC `xml:"c"`
  208. }
  209. // xlsxC directly maps the c element in the namespace
  210. // http://schemas.openxmlformats.org/sprceadsheetml/2006/main -
  211. // currently I have not checked it for completeness - it does as much
  212. // as I need.
  213. type xlsxC struct {
  214. R string `xml:"r,attr"` // Cell ID, e.g. A1
  215. S int `xml:"s,attr"` // Style reference.
  216. T string `xml:"t,attr,omitempty"` // Type.
  217. V string `xml:"v"` // Value
  218. F *xlsxF `xml:"f,omitempty"` // Formula
  219. }
  220. // xlsxC directly maps the f element in the namespace
  221. // http://schemas.openxmlformats.org/sprceadsheetml/2006/main -
  222. // currently I have not checked it for completeness - it does as much
  223. // as I need.
  224. type xlsxF struct {
  225. Content string `xml:",chardata"`
  226. T string `xml:"t,attr,omitempty"` // Formula type
  227. Ref string `xml:"ref,attr,omitempty"` // Shared formula ref
  228. Si int `xml:"si,attr,omitempty"` // Shared formula index
  229. }
  230. // Create a new XLSX Worksheet with default values populated.
  231. // Strictly for internal use only!
  232. func newXlsxWorksheet() (worksheet *xlsxWorksheet) {
  233. worksheet = &xlsxWorksheet{}
  234. worksheet.SheetPr.FilterMode = false
  235. worksheet.SheetPr.PageSetUpPr = make([]xlsxPageSetUpPr, 1)
  236. worksheet.SheetPr.PageSetUpPr[0] = xlsxPageSetUpPr{FitToPage: false}
  237. worksheet.SheetViews.SheetView = make([]xlsxSheetView, 1)
  238. worksheet.SheetViews.SheetView[0] = xlsxSheetView{
  239. ColorId: 64,
  240. DefaultGridColor: true,
  241. RightToLeft: false,
  242. Selection: make([]xlsxSelection, 1),
  243. ShowFormulas: false,
  244. ShowGridLines: true,
  245. ShowOutlineSymbols: true,
  246. ShowRowColHeaders: true,
  247. ShowZeros: true,
  248. TabSelected: true,
  249. TopLeftCell: "A1",
  250. View: "normal",
  251. WindowProtection: false,
  252. WorkbookViewId: 0,
  253. ZoomScale: 100,
  254. ZoomScaleNormal: 100,
  255. ZoomScalePageLayoutView: 100}
  256. worksheet.SheetViews.SheetView[0].Selection[0] = xlsxSelection{
  257. Pane: "topLeft",
  258. ActiveCell: "A1",
  259. ActiveCellId: 0,
  260. SQRef: "A1"}
  261. worksheet.SheetFormatPr.DefaultRowHeight = 12.85
  262. worksheet.PrintOptions.Headings = false
  263. worksheet.PrintOptions.GridLines = false
  264. worksheet.PrintOptions.GridLinesSet = true
  265. worksheet.PrintOptions.HorizontalCentered = false
  266. worksheet.PrintOptions.VerticalCentered = false
  267. worksheet.PageMargins.Left = 0.7875
  268. worksheet.PageMargins.Right = 0.7875
  269. worksheet.PageMargins.Top = 1.05277777777778
  270. worksheet.PageMargins.Bottom = 1.05277777777778
  271. worksheet.PageMargins.Header = 0.7875
  272. worksheet.PageMargins.Footer = 0.7875
  273. worksheet.PageSetUp.PaperSize = "9"
  274. worksheet.PageSetUp.Scale = 100
  275. worksheet.PageSetUp.FirstPageNumber = 1
  276. worksheet.PageSetUp.FitToWidth = 1
  277. worksheet.PageSetUp.FitToHeight = 1
  278. worksheet.PageSetUp.PageOrder = "downThenOver"
  279. worksheet.PageSetUp.Orientation = "portrait"
  280. worksheet.PageSetUp.UsePrinterDefaults = false
  281. worksheet.PageSetUp.BlackAndWhite = false
  282. worksheet.PageSetUp.Draft = false
  283. worksheet.PageSetUp.CellComments = "none"
  284. worksheet.PageSetUp.UseFirstPageNumber = true
  285. worksheet.PageSetUp.HorizontalDPI = 300
  286. worksheet.PageSetUp.VerticalDPI = 300
  287. worksheet.PageSetUp.Copies = 1
  288. worksheet.HeaderFooter.OddHeader = make([]xlsxOddHeader, 1)
  289. worksheet.HeaderFooter.OddHeader[0] = xlsxOddHeader{Content: `&C&"Times New Roman,Regular"&12&A`}
  290. worksheet.HeaderFooter.OddFooter = make([]xlsxOddFooter, 1)
  291. worksheet.HeaderFooter.OddFooter[0] = xlsxOddFooter{Content: `&C&"Times New Roman,Regular"&12Page &P`}
  292. return
  293. }