xlsx.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package utils
  2. import (
  3. "github.com/tealeg/xlsx/v3"
  4. )
  5. type StyleCfg struct {
  6. IsBold bool
  7. IsBorderTop bool
  8. IsBorderRight bool
  9. IsBorderLeft bool
  10. IsBorderBottom bool
  11. FontSize float64
  12. Horizontal string
  13. Vertical string
  14. Font string
  15. Fill []string
  16. }
  17. func GetCommonStyle(cfg StyleCfg) *xlsx.Style {
  18. styleCommon := xlsx.NewStyle()
  19. if cfg.Horizontal == "" {
  20. cfg.Horizontal = "center"
  21. }
  22. if cfg.Vertical == "" {
  23. cfg.Vertical = "center"
  24. }
  25. if cfg.Font == "" {
  26. cfg.Font = "微软雅黑"
  27. }
  28. styleCommon.Alignment.Horizontal = cfg.Horizontal
  29. styleCommon.Alignment.Vertical = cfg.Vertical
  30. styleCommon.Alignment.WrapText = true
  31. styleCommon.Font.Name = cfg.Font
  32. if cfg.FontSize == 0 {
  33. cfg.FontSize = 11
  34. }
  35. styleCommon.Font.Size = cfg.FontSize
  36. if cfg.IsBold {
  37. styleCommon.Font.Bold = true
  38. }
  39. if len(cfg.Fill) > 0 {
  40. styleCommon.Fill = *xlsx.NewFill("solid", cfg.Fill[0], cfg.Fill[1])
  41. }
  42. styleCommon.Border.Top = "thin"
  43. styleCommon.Border.TopColor = "00000000"
  44. styleCommon.Border.Right = "thin"
  45. styleCommon.Border.RightColor = "00000000"
  46. styleCommon.Border.Bottom = "thin"
  47. styleCommon.Border.BottomColor = "00000000"
  48. styleCommon.Border.Left = "thin"
  49. styleCommon.Border.LeftColor = "00000000"
  50. if cfg.IsBorderTop {
  51. styleCommon.Border.Top = "medium"
  52. }
  53. if cfg.IsBorderRight {
  54. styleCommon.Border.Right = "medium"
  55. }
  56. if cfg.IsBorderBottom {
  57. styleCommon.Border.Bottom = "medium"
  58. }
  59. if cfg.IsBorderLeft {
  60. styleCommon.Border.Left = "medium"
  61. }
  62. styleCommon.ApplyBorder = true
  63. styleCommon.ApplyFont = true
  64. return styleCommon
  65. }
  66. func MergeCell(row *xlsx.Row, style *xlsx.Style, num int) {
  67. for i := 0; i < num; i++ {
  68. cell := row.AddCell()
  69. cell.SetStyle(style)
  70. }
  71. }
  72. type Row struct {
  73. Height float64
  74. }
  75. func AddRow(sh *xlsx.Sheet, row Row) *xlsx.Row {
  76. r := sh.AddRow()
  77. r.SetHeight(row.Height)
  78. return r
  79. }
  80. type Cell struct {
  81. Float float64
  82. Value string
  83. Format string
  84. Style *xlsx.Style
  85. HMerge int
  86. VMerge int
  87. }
  88. func AddCell(row *xlsx.Row, cell Cell) {
  89. c := row.AddCell()
  90. switch cell.Format {
  91. case "float":
  92. c.SetFloat(cell.Float)
  93. default:
  94. c.Value = cell.Value
  95. }
  96. if cell.HMerge > 0 {
  97. c.HMerge = cell.HMerge
  98. MergeCell(row, cell.Style, cell.HMerge)
  99. }
  100. if cell.VMerge > 0 {
  101. c.VMerge = cell.VMerge
  102. }
  103. c.SetStyle(cell.Style)
  104. }
  105. func SetColWidth(sh *xlsx.Sheet, width []float64) *xlsx.Sheet {
  106. for k, v := range width {
  107. idx := k + 1
  108. sh.SetColWidth(idx, idx, v)
  109. }
  110. return sh
  111. }