userExcel.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package api
  2. import (
  3. "copter-train/db/model"
  4. "fmt"
  5. "strings"
  6. "github.com/xuri/excelize/v2"
  7. )
  8. type UserExcel struct {
  9. Offset int
  10. Row int
  11. Title string //标题
  12. Excel *excelize.File
  13. SheetName string
  14. AlignCenterStyle int
  15. Content []*model.User
  16. RowMap map[string]int
  17. RowWidthArray []float64
  18. RowsHeightArray []map[int]float64
  19. }
  20. // 批量设置行高
  21. func (b *UserExcel) setRowsHeight() {
  22. for _, rowHeight := range b.RowsHeightArray {
  23. for row, height := range rowHeight {
  24. b.Excel.SetRowHeight(b.SheetName, row, height)
  25. }
  26. }
  27. }
  28. // 获取范围内单元格的宽度 A:F
  29. func (b *UserExcel) getRangeWidth(r string) float64 {
  30. rg := strings.Split(r, ":")
  31. if len(rg) == 1 {
  32. start := b.RowMap[rg[0]]
  33. return b.RowWidthArray[start]
  34. } else if len(rg) == 2 {
  35. start := b.RowMap[rg[0]]
  36. end := b.RowMap[rg[1]]
  37. rowr := b.RowWidthArray[start : end+1]
  38. width := 0.0
  39. for _, v := range rowr {
  40. width += v
  41. }
  42. return width
  43. }
  44. return 0.0
  45. }
  46. func (b *UserExcel) drawTitle() error {
  47. b.Row++
  48. startCell := fmt.Sprintf("A%d", b.Row)
  49. endCell := fmt.Sprintf("D%d", b.Row)
  50. b.RowMap = map[string]int{"A": 0, "B": 1, "C": 2, "D": 3}
  51. b.RowWidthArray = []float64{12, 12, 12, 36}
  52. b.Excel.SetColWidth(b.SheetName, "A", "A", 12)
  53. b.Excel.SetColWidth(b.SheetName, "B", "B", 12)
  54. b.Excel.SetColWidth(b.SheetName, "C", "C", 12)
  55. b.Excel.SetColWidth(b.SheetName, "D", "D", 36)
  56. err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
  57. if err != nil {
  58. return err
  59. }
  60. style, err := b.Excel.NewStyle(&excelize.Style{
  61. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  62. Font: &excelize.Font{Bold: true, Size: 18}})
  63. if err != nil {
  64. return err
  65. }
  66. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  67. if err != nil {
  68. return err
  69. }
  70. b.Excel.SetRowHeight(b.SheetName, b.Row, 26)
  71. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  72. return nil
  73. }
  74. func (b *UserExcel) drawTableTitle() error {
  75. b.Row++
  76. var drawCol = func(prefix string, value string) error {
  77. cell := fmt.Sprintf("%s%d", prefix, b.Row)
  78. err := b.Excel.SetCellStyle(b.SheetName, cell, cell, b.AlignCenterStyle)
  79. if err != nil {
  80. return err
  81. }
  82. return b.Excel.SetCellValue(b.SheetName, cell, value)
  83. }
  84. drawCol("A", "用户名")
  85. drawCol("B", "编号")
  86. drawCol("C", "角色")
  87. drawCol("D", "密码")
  88. b.Excel.SetRowHeight(b.SheetName, b.Row, 22)
  89. return nil
  90. }
  91. func (b *UserExcel) drawTableContent() error {
  92. b.Row++
  93. var DrawRow = func(rowIndex int, values ...string) float64 {
  94. charas := []string{"A", "B", "C", "D"}
  95. // 获取该行最大行高
  96. max := getRowHeight(values[0], b.getRangeWidth(charas[0]))
  97. for i, c := range charas {
  98. v := ""
  99. if i < len(values) {
  100. v = values[i]
  101. }
  102. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  103. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  104. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  105. if getRowHeight(v, b.getRangeWidth(c)) > max {
  106. max = getRowHeight(v, b.getRangeWidth(c))
  107. }
  108. }
  109. return max
  110. }
  111. users := b.Content
  112. if len(users) > 0 {
  113. for _, user := range users {
  114. rowMaxHeight := DrawRow(b.Row, user.Name, user.Nid, user.Roles[0], user.Password)
  115. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: rowMaxHeight})
  116. b.Row++
  117. }
  118. }
  119. return nil
  120. }
  121. func (b *UserExcel) Draws() {
  122. b.drawTitle()
  123. b.drawTableTitle()
  124. b.drawTableContent()
  125. // 设置行高
  126. b.setRowsHeight()
  127. }
  128. func NewUserExcel(f *excelize.File) *UserExcel {
  129. border := []excelize.Border{
  130. {Type: "top", Style: 1, Color: "000000"},
  131. {Type: "left", Style: 1, Color: "000000"},
  132. {Type: "right", Style: 1, Color: "000000"},
  133. {Type: "bottom", Style: 1, Color: "000000"},
  134. }
  135. styleLeft, _ := f.NewStyle(&excelize.Style{
  136. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  137. Border: border,
  138. })
  139. b := &UserExcel{
  140. Title: "用户信息",
  141. SheetName: "Sheet1",
  142. Excel: f,
  143. Offset: 0,
  144. AlignCenterStyle: styleLeft,
  145. RowMap: map[string]int{"A": 0, "B": 1, "C": 2, "D": 3},
  146. RowWidthArray: []float64{12, 12, 12, 20},
  147. RowsHeightArray: make([]map[int]float64, 0),
  148. }
  149. // f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  150. return b
  151. }
  152. func (b *UserExcel) FormatToEmpty(str *string) {
  153. if *str == "0" || *str == "0.000" {
  154. *str = "-"
  155. }
  156. }