xmlWorksheet.go 13 KB

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