stream_style.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package xlsx
  2. // StreamStyle has style and formatting information.
  3. // Used to store a style for streaming
  4. type StreamStyle struct {
  5. xNumFmtId int
  6. style *Style
  7. }
  8. const (
  9. GeneralFormat = 0
  10. IntegerFormat = 1
  11. DecimalFormat = 2
  12. DateFormat_dd_mm_yy = 14
  13. DateTimeFormat_d_m_yy_h_mm = 22
  14. )
  15. var (
  16. StreamStyleFromColumn StreamStyle
  17. StreamStyleDefaultString StreamStyle
  18. StreamStyleBoldString StreamStyle
  19. StreamStyleItalicString StreamStyle
  20. StreamStyleUnderlinedString StreamStyle
  21. StreamStyleDefaultInteger StreamStyle
  22. StreamStyleBoldInteger StreamStyle
  23. StreamStyleItalicInteger StreamStyle
  24. StreamStyleUnderlinedInteger StreamStyle
  25. StreamStyleDefaultDate StreamStyle
  26. StreamStyleDefaultDecimal StreamStyle
  27. )
  28. var (
  29. FontBold *Font
  30. FontItalic *Font
  31. FontUnderlined *Font
  32. )
  33. var (
  34. FillGreen *Fill
  35. FillRed *Fill
  36. FillWhite *Fill
  37. )
  38. func init() {
  39. // Init Fonts
  40. FontBold = DefaultFont()
  41. FontBold.Bold = true
  42. FontItalic = DefaultFont()
  43. FontItalic.Italic = true
  44. FontUnderlined = DefaultFont()
  45. FontUnderlined.Underline = true
  46. // Init Fills
  47. FillGreen = NewFill(Solid_Cell_Fill, RGB_Light_Green, RGB_White)
  48. FillRed = NewFill(Solid_Cell_Fill, RGB_Light_Red, RGB_White)
  49. FillWhite = DefaultFill()
  50. // Init default string styles
  51. StreamStyleDefaultString = MakeStringStyle(DefaultFont(), DefaultFill(), DefaultAlignment(), DefaultBorder())
  52. StreamStyleBoldString = MakeStringStyle(FontBold, DefaultFill(), DefaultAlignment(), DefaultBorder())
  53. StreamStyleItalicString = MakeStringStyle(FontItalic, DefaultFill(), DefaultAlignment(), DefaultBorder())
  54. StreamStyleUnderlinedString = MakeStringStyle(FontUnderlined, DefaultFill(), DefaultAlignment(), DefaultBorder())
  55. // Init default Integer styles
  56. StreamStyleDefaultInteger = MakeIntegerStyle(DefaultFont(), DefaultFill(), DefaultAlignment(), DefaultBorder())
  57. StreamStyleBoldInteger = MakeIntegerStyle(FontBold, DefaultFill(), DefaultAlignment(), DefaultBorder())
  58. StreamStyleItalicInteger = MakeIntegerStyle(FontItalic, DefaultFill(), DefaultAlignment(), DefaultBorder())
  59. StreamStyleUnderlinedInteger = MakeIntegerStyle(FontUnderlined, DefaultFill(), DefaultAlignment(), DefaultBorder())
  60. StreamStyleDefaultDate = MakeDateStyle(DefaultFont(), DefaultFill(), DefaultAlignment(), DefaultBorder())
  61. StreamStyleDefaultDecimal = MakeDecimalStyle(DefaultFont(), DefaultFill(), DefaultAlignment(), DefaultBorder())
  62. DefaultStringCellMetadata = CellMetadata{CellTypeString, StreamStyleDefaultString}
  63. DefaultNumericCellMetadata = CellMetadata{CellTypeNumeric, StreamStyleDefaultString}
  64. DefaultDecimalCellMetadata = CellMetadata{CellTypeNumeric, StreamStyleDefaultDecimal}
  65. DefaultIntegerCellMetadata = CellMetadata{CellTypeNumeric, StreamStyleDefaultInteger}
  66. DefaultDateCellMetadata = CellMetadata{CellTypeDate, StreamStyleDefaultDate}
  67. }
  68. // MakeStyle creates a new StreamStyle and add it to the styles that will be streamed.
  69. func MakeStyle(numFormatId int, font *Font, fill *Fill, alignment *Alignment, border *Border) StreamStyle {
  70. newStyle := NewStyle()
  71. newStyle.Font = *font
  72. newStyle.Fill = *fill
  73. newStyle.Alignment = *alignment
  74. newStyle.Border = *border
  75. newStyle.ApplyFont = true
  76. newStyle.ApplyFill = true
  77. newStyle.ApplyAlignment = true
  78. newStyle.ApplyBorder = true
  79. newStreamStyle := StreamStyle{
  80. xNumFmtId: numFormatId,
  81. style: newStyle,
  82. }
  83. return newStreamStyle
  84. }
  85. // MakeStringStyle creates a new style that can be used on cells with string data.
  86. // If used on other data the formatting might be wrong.
  87. func MakeStringStyle(font *Font, fill *Fill, alignment *Alignment, border *Border) StreamStyle {
  88. return MakeStyle(GeneralFormat, font, fill, alignment, border)
  89. }
  90. // MakeIntegerStyle creates a new style that can be used on cells with integer data.
  91. // If used on other data the formatting might be wrong.
  92. func MakeIntegerStyle(font *Font, fill *Fill, alignment *Alignment, border *Border) StreamStyle {
  93. return MakeStyle(IntegerFormat, font, fill, alignment, border)
  94. }
  95. // MakeDecimalStyle creates a new style that can be used on cells with decimal numeric data.
  96. // If used on other data the formatting might be wrong.
  97. func MakeDecimalStyle(font *Font, fill *Fill, alignment *Alignment, border *Border) StreamStyle {
  98. return MakeStyle(DecimalFormat, font, fill, alignment, border)
  99. }
  100. // MakeDateStyle creates a new style that can be used on cells with Date data.
  101. // The formatting used is: dd_mm_yy
  102. // If used on other data the formatting might be wrong.
  103. func MakeDateStyle(font *Font, fill *Fill, alignment *Alignment, border *Border) StreamStyle {
  104. return MakeStyle(DateFormat_dd_mm_yy, font, fill, alignment, border)
  105. }