xmlWorksheet.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. DefaultRowHeight float64 `xml:"defaultRowHeight,attr"`
  96. }
  97. // xlsxSheetViews directly maps the sheetViews element in the namespace
  98. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  99. // currently I have not checked it for completeness - it does as much
  100. // as I need.
  101. type xlsxSheetViews struct {
  102. SheetView []xlsxSheetView `xml:"sheetView"`
  103. }
  104. // xlsxSheetView directly maps the sheetView element in the namespace
  105. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  106. // currently I have not checked it for completeness - it does as much
  107. // as I need.
  108. type xlsxSheetView struct {
  109. WindowProtection bool `xml:"windowProtection,attr"`
  110. ShowFormulas bool `xml:"showFormulas,attr"`
  111. ShowGridLines bool `xml:"showGridLines,attr"`
  112. ShowRowColHeaders bool `xml:"showRowColHeaders,attr"`
  113. ShowZeros bool `xml:"showZeros,attr"`
  114. RightToLeft bool `xml:"rightToLeft,attr"`
  115. TabSelected bool `xml:"tabSelected,attr"`
  116. ShowOutlineSymbols bool `xml:"showOutlineSymbols,attr"`
  117. DefaultGridColor bool `xml:"defaultGridColor,attr"`
  118. View string `xml:"view,attr"`
  119. TopLeftCell string `xml:"topLeftCell,attr"`
  120. ColorId int `xml:"colorId,attr"`
  121. ZoomScale float64 `xml:"zoomScale,attr"`
  122. ZoomScaleNormal float64 `xml:"zoomScaleNormal,attr"`
  123. ZoomScalePageLayoutView float64 `xml:"zoomScalePageLayoutView,attr"`
  124. WorkbookViewId int `xml:"workbookViewId,attr"`
  125. Selection []xlsxSelection `xml:"selection"`
  126. Pane *xlsxPane `xml:"pane"`
  127. }
  128. // xlsxSelection directly maps the selection element in the namespace
  129. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  130. // currently I have not checked it for completeness - it does as much
  131. // as I need.
  132. type xlsxSelection struct {
  133. Pane string `xml:"pane,attr"`
  134. ActiveCell string `xml:"activeCell,attr"`
  135. ActiveCellId int `xml:"activeCellId,attr"`
  136. SQRef string `xml:"sqref,attr"`
  137. }
  138. // xlsxSelection directly maps the selection element in the namespace
  139. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  140. // currently I have not checked it for completeness - it does as much
  141. // as I need.
  142. type xlsxPane struct {
  143. XSplit int `xml:"xSplit,attr"`
  144. YSplit int `xml:"ySplit,attr"`
  145. TopLeftCell string `xml:"topLeftCell,attr"`
  146. ActivePane string `xml:"activePane,attr"`
  147. State string `xml:"state,attr"` // Either "split" or "frozen"
  148. }
  149. // xlsxSheetPr directly maps the sheetPr element in the namespace
  150. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  151. // currently I have not checked it for completeness - it does as much
  152. // as I need.
  153. type xlsxSheetPr struct {
  154. FilterMode bool `xml:"filterMode,attr"`
  155. PageSetUpPr []xlsxPageSetUpPr `xml:"pageSetUpPr"`
  156. }
  157. // xlsxPageSetUpPr directly maps the pageSetupPr element in the namespace
  158. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  159. // currently I have not checked it for completeness - it does as much
  160. // as I need.
  161. type xlsxPageSetUpPr struct {
  162. FitToPage bool `xml:"fitToPage,attr"`
  163. }
  164. // xlsxCols directly maps the cols element in the namespace
  165. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  166. // currently I have not checked it for completeness - it does as much
  167. // as I need.
  168. type xlsxCols struct {
  169. Col []xlsxCol `xml:"col"`
  170. }
  171. // xlsxCol directly maps the col element in the namespace
  172. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  173. // currently I have not checked it for completeness - it does as much
  174. // as I need.
  175. type xlsxCol struct {
  176. Collapsed bool `xml:"collapsed,attr"`
  177. Hidden bool `xml:"hidden,attr"`
  178. Max int `xml:"max,attr"`
  179. Min int `xml:"min,attr"`
  180. // Style int `xml:"style,attr"`
  181. Width float64 `xml:"width,attr"`
  182. }
  183. // xlsxDimension directly maps the dimension element in the namespace
  184. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  185. // currently I have not checked it for completeness - it does as much
  186. // as I need.
  187. type xlsxDimension struct {
  188. Ref string `xml:"ref,attr"`
  189. }
  190. // xlsxSheetData directly maps the sheetData element in the namespace
  191. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  192. // currently I have not checked it for completeness - it does as much
  193. // as I need.
  194. type xlsxSheetData struct {
  195. XMLName xml.Name `xml:"sheetData"`
  196. Row []xlsxRow `xml:"row"`
  197. }
  198. // xlsxRow directly maps the row element in the namespace
  199. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  200. // currently I have not checked it for completeness - it does as much
  201. // as I need.
  202. type xlsxRow struct {
  203. R int `xml:"r,attr"`
  204. Spans string `xml:"spans,attr,omitempty"`
  205. Hidden bool `xml:"hidden,attr,omitempty"`
  206. C []xlsxC `xml:"c"`
  207. }
  208. // xlsxC directly maps the c element in the namespace
  209. // http://schemas.openxmlformats.org/sprceadsheetml/2006/main -
  210. // currently I have not checked it for completeness - it does as much
  211. // as I need.
  212. type xlsxC struct {
  213. R string `xml:"r,attr"` // Cell ID, e.g. A1
  214. S int `xml:"s,attr"` // Style reference.
  215. T string `xml:"t,attr,omitempty"` // Type.
  216. V string `xml:"v"` // Value
  217. F *xlsxF `xml:"f,omitempty"` // Formula
  218. }
  219. // xlsxC directly maps the f element in the namespace
  220. // http://schemas.openxmlformats.org/sprceadsheetml/2006/main -
  221. // currently I have not checked it for completeness - it does as much
  222. // as I need.
  223. type xlsxF struct {
  224. Content string `xml:",chardata"`
  225. T string `xml:"t,attr,omitempty"` // Formula type
  226. Ref string `xml:"ref,attr,omitempty"` // Shared formula ref
  227. Si int `xml:"si,attr,omitempty"` // Shared formula index
  228. }
  229. // Create a new XLSX Worksheet with default values populated.
  230. // Strictly for internal use only!
  231. func newXlsxWorksheet() (worksheet *xlsxWorksheet) {
  232. worksheet = &xlsxWorksheet{}
  233. worksheet.SheetPr.FilterMode = false
  234. worksheet.SheetPr.PageSetUpPr = make([]xlsxPageSetUpPr, 1)
  235. worksheet.SheetPr.PageSetUpPr[0] = xlsxPageSetUpPr{FitToPage: false}
  236. worksheet.SheetViews.SheetView = make([]xlsxSheetView, 1)
  237. worksheet.SheetViews.SheetView[0] = xlsxSheetView{
  238. ColorId: 64,
  239. DefaultGridColor: true,
  240. RightToLeft: false,
  241. Selection: make([]xlsxSelection, 1),
  242. ShowFormulas: false,
  243. ShowGridLines: true,
  244. ShowOutlineSymbols: true,
  245. ShowRowColHeaders: true,
  246. ShowZeros: true,
  247. TabSelected: true,
  248. TopLeftCell: "A1",
  249. View: "normal",
  250. WindowProtection: false,
  251. WorkbookViewId: 0,
  252. ZoomScale: 100,
  253. ZoomScaleNormal: 100,
  254. ZoomScalePageLayoutView: 100}
  255. worksheet.SheetViews.SheetView[0].Selection[0] = xlsxSelection{
  256. Pane: "topLeft",
  257. ActiveCell: "A1",
  258. ActiveCellId: 0,
  259. SQRef: "A1"}
  260. worksheet.SheetFormatPr.DefaultRowHeight = 12.85
  261. worksheet.PrintOptions.Headings = false
  262. worksheet.PrintOptions.GridLines = false
  263. worksheet.PrintOptions.GridLinesSet = true
  264. worksheet.PrintOptions.HorizontalCentered = false
  265. worksheet.PrintOptions.VerticalCentered = false
  266. worksheet.PageMargins.Left = 0.7875
  267. worksheet.PageMargins.Right = 0.7875
  268. worksheet.PageMargins.Top = 1.05277777777778
  269. worksheet.PageMargins.Bottom = 1.05277777777778
  270. worksheet.PageMargins.Header = 0.7875
  271. worksheet.PageMargins.Footer = 0.7875
  272. worksheet.PageSetUp.PaperSize = "9"
  273. worksheet.PageSetUp.Scale = 100
  274. worksheet.PageSetUp.FirstPageNumber = 1
  275. worksheet.PageSetUp.FitToWidth = 1
  276. worksheet.PageSetUp.FitToHeight = 1
  277. worksheet.PageSetUp.PageOrder = "downThenOver"
  278. worksheet.PageSetUp.Orientation = "portrait"
  279. worksheet.PageSetUp.UsePrinterDefaults = false
  280. worksheet.PageSetUp.BlackAndWhite = false
  281. worksheet.PageSetUp.Draft = false
  282. worksheet.PageSetUp.CellComments = "none"
  283. worksheet.PageSetUp.UseFirstPageNumber = true
  284. worksheet.PageSetUp.HorizontalDPI = 300
  285. worksheet.PageSetUp.VerticalDPI = 300
  286. worksheet.PageSetUp.Copies = 1
  287. worksheet.HeaderFooter.OddHeader = make([]xlsxOddHeader, 1)
  288. worksheet.HeaderFooter.OddHeader[0] = xlsxOddHeader{Content: `&C&"Times New Roman,Regular"&12&A`}
  289. worksheet.HeaderFooter.OddFooter = make([]xlsxOddFooter, 1)
  290. worksheet.HeaderFooter.OddFooter[0] = xlsxOddFooter{Content: `&C&"Times New Roman,Regular"&12Page &P`}
  291. return
  292. }