xmlWorksheet.go 13 KB

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