xmlWorksheet.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. 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 int `xml:"xSplit,attr"`
  145. YSplit int `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. type xlsxMergeCell struct {
  210. Ref string `xml:"ref,attr"` // ref: horiz "A1:C1", vert "B3:B6", both "D3:G4"
  211. }
  212. type xlsxMergeCells struct {
  213. XMLName xml.Name //`xml:"mergeCells,omitempty"`
  214. Count int `xml:"count,attr,omitempty"`
  215. Cells []xlsxMergeCell `xml:"mergeCell,omitempty"`
  216. }
  217. // xlsxC directly maps the c element in the namespace
  218. // http://schemas.openxmlformats.org/sprceadsheetml/2006/main -
  219. // currently I have not checked it for completeness - it does as much
  220. // as I need.
  221. type xlsxC struct {
  222. R string `xml:"r,attr"` // Cell ID, e.g. A1
  223. S int `xml:"s,attr,omitempty"` // Style reference.
  224. T string `xml:"t,attr,omitempty"` // Type.
  225. V string `xml:"v"` // Value
  226. F *xlsxF `xml:"f,omitempty"` // Formula
  227. }
  228. // xlsxC directly maps the f element in the namespace
  229. // http://schemas.openxmlformats.org/sprceadsheetml/2006/main -
  230. // currently I have not checked it for completeness - it does as much
  231. // as I need.
  232. type xlsxF struct {
  233. Content string `xml:",chardata"`
  234. T string `xml:"t,attr,omitempty"` // Formula type
  235. Ref string `xml:"ref,attr,omitempty"` // Shared formula ref
  236. Si int `xml:"si,attr,omitempty"` // Shared formula index
  237. }
  238. // Create a new XLSX Worksheet with default values populated.
  239. // Strictly for internal use only!
  240. func newXlsxWorksheet() (worksheet *xlsxWorksheet) {
  241. worksheet = &xlsxWorksheet{}
  242. worksheet.SheetPr.FilterMode = false
  243. worksheet.SheetPr.PageSetUpPr = make([]xlsxPageSetUpPr, 1)
  244. worksheet.SheetPr.PageSetUpPr[0] = xlsxPageSetUpPr{FitToPage: false}
  245. worksheet.SheetViews.SheetView = make([]xlsxSheetView, 1)
  246. worksheet.SheetViews.SheetView[0] = xlsxSheetView{
  247. ColorId: 64,
  248. DefaultGridColor: true,
  249. RightToLeft: false,
  250. Selection: make([]xlsxSelection, 1),
  251. ShowFormulas: false,
  252. ShowGridLines: true,
  253. ShowOutlineSymbols: true,
  254. ShowRowColHeaders: true,
  255. ShowZeros: true,
  256. TabSelected: true,
  257. TopLeftCell: "A1",
  258. View: "normal",
  259. WindowProtection: false,
  260. WorkbookViewId: 0,
  261. ZoomScale: 100,
  262. ZoomScaleNormal: 100,
  263. ZoomScalePageLayoutView: 100}
  264. worksheet.SheetViews.SheetView[0].Selection[0] = xlsxSelection{
  265. Pane: "topLeft",
  266. ActiveCell: "A1",
  267. ActiveCellId: 0,
  268. SQRef: "A1"}
  269. worksheet.SheetFormatPr.DefaultRowHeight = 12.85
  270. worksheet.PrintOptions.Headings = false
  271. worksheet.PrintOptions.GridLines = false
  272. worksheet.PrintOptions.GridLinesSet = true
  273. worksheet.PrintOptions.HorizontalCentered = false
  274. worksheet.PrintOptions.VerticalCentered = false
  275. worksheet.PageMargins.Left = 0.7875
  276. worksheet.PageMargins.Right = 0.7875
  277. worksheet.PageMargins.Top = 1.05277777777778
  278. worksheet.PageMargins.Bottom = 1.05277777777778
  279. worksheet.PageMargins.Header = 0.7875
  280. worksheet.PageMargins.Footer = 0.7875
  281. worksheet.PageSetUp.PaperSize = "9"
  282. worksheet.PageSetUp.Scale = 100
  283. worksheet.PageSetUp.FirstPageNumber = 1
  284. worksheet.PageSetUp.FitToWidth = 1
  285. worksheet.PageSetUp.FitToHeight = 1
  286. worksheet.PageSetUp.PageOrder = "downThenOver"
  287. worksheet.PageSetUp.Orientation = "portrait"
  288. worksheet.PageSetUp.UsePrinterDefaults = false
  289. worksheet.PageSetUp.BlackAndWhite = false
  290. worksheet.PageSetUp.Draft = false
  291. worksheet.PageSetUp.CellComments = "none"
  292. worksheet.PageSetUp.UseFirstPageNumber = true
  293. worksheet.PageSetUp.HorizontalDPI = 300
  294. worksheet.PageSetUp.VerticalDPI = 300
  295. worksheet.PageSetUp.Copies = 1
  296. worksheet.HeaderFooter.OddHeader = make([]xlsxOddHeader, 1)
  297. worksheet.HeaderFooter.OddHeader[0] = xlsxOddHeader{Content: `&C&"Times New Roman,Regular"&12&A`}
  298. worksheet.HeaderFooter.OddFooter = make([]xlsxOddFooter, 1)
  299. worksheet.HeaderFooter.OddFooter[0] = xlsxOddFooter{Content: `&C&"Times New Roman,Regular"&12Page &P`}
  300. return
  301. }