latestExamExcel.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package api
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/xuri/excelize/v2"
  7. )
  8. type LatestExamExcel struct {
  9. Offset int
  10. Row int
  11. Title string //标题
  12. Excel *excelize.File
  13. SheetName string
  14. AlignCenterStyle int
  15. Content []*HandleLatestExamLog
  16. RowMap map[string]int
  17. RowWidthArray []float64
  18. RowsHeightArray []map[int]float64
  19. }
  20. // 批量设置行高
  21. func (b *LatestExamExcel) 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 *LatestExamExcel) 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 *LatestExamExcel) drawTitle() error {
  47. b.Row++
  48. startCell := fmt.Sprintf("A%d", b.Row)
  49. endCell := fmt.Sprintf("G%d", b.Row)
  50. b.RowMap = map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6}
  51. b.RowWidthArray = []float64{16, 12, 12, 10, 10, 12, 16}
  52. b.Excel.SetColWidth(b.SheetName, "A", "A", 16)
  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", 10)
  56. b.Excel.SetColWidth(b.SheetName, "E", "E", 10)
  57. b.Excel.SetColWidth(b.SheetName, "F", "F", 12)
  58. b.Excel.SetColWidth(b.SheetName, "G", "G", 16)
  59. err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
  60. if err != nil {
  61. return err
  62. }
  63. style, err := b.Excel.NewStyle(&excelize.Style{
  64. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  65. Font: &excelize.Font{Bold: true, Size: 18}})
  66. if err != nil {
  67. return err
  68. }
  69. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  70. if err != nil {
  71. return err
  72. }
  73. b.Excel.SetRowHeight(b.SheetName, b.Row, 26)
  74. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  75. return nil
  76. }
  77. func (b *LatestExamExcel) drawTableTitle() error {
  78. b.Row++
  79. var drawCol = func(prefix string, value string) error {
  80. cell := fmt.Sprintf("%s%d", prefix, b.Row)
  81. err := b.Excel.SetCellStyle(b.SheetName, cell, cell, b.AlignCenterStyle)
  82. if err != nil {
  83. return err
  84. }
  85. return b.Excel.SetCellValue(b.SheetName, cell, value)
  86. }
  87. drawCol("A", "考试日期")
  88. drawCol("B", "用户名")
  89. drawCol("C", "编码")
  90. drawCol("D", "类型")
  91. drawCol("E", "分数")
  92. drawCol("F", "学习进度(%)")
  93. drawCol("G", "学习总时长(分钟)")
  94. b.Excel.SetRowHeight(b.SheetName, b.Row, 22)
  95. return nil
  96. }
  97. func (b *LatestExamExcel) drawTableContent() error {
  98. b.Row++
  99. var DrawRow = func(rowIndex int, values ...string) float64 {
  100. charas := []string{"A", "B", "C", "D", "E", "F", "G"}
  101. // 获取该行最大行高
  102. max := getRowHeight(values[0], b.getRangeWidth(charas[0]))
  103. for i, c := range charas {
  104. v := ""
  105. if i < len(values) {
  106. v = values[i]
  107. }
  108. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  109. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  110. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  111. if getRowHeight(v, b.getRangeWidth(c)) > max {
  112. max = getRowHeight(v, b.getRangeWidth(c))
  113. }
  114. }
  115. return max
  116. }
  117. latestExamLogs := b.Content
  118. if len(latestExamLogs) > 0 {
  119. for _, examLog := range latestExamLogs {
  120. score := strconv.Itoa(examLog.Score)
  121. completeRate := fmt.Sprintf("%d%%", examLog.CompleteRate)
  122. learnTime := strconv.Itoa(examLog.LearnTime)
  123. rowMaxHeight := DrawRow(b.Row, examLog.ExamDate, examLog.UserName, examLog.Nid, examLog.Type, score,
  124. completeRate, learnTime)
  125. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: rowMaxHeight})
  126. b.Row++
  127. }
  128. }
  129. return nil
  130. }
  131. func (b *LatestExamExcel) Draws() {
  132. b.drawTitle()
  133. b.drawTableTitle()
  134. b.drawTableContent()
  135. // 设置行高
  136. b.setRowsHeight()
  137. }
  138. func NewLatestExamExcel(f *excelize.File) *LatestExamExcel {
  139. border := []excelize.Border{
  140. {Type: "top", Style: 1, Color: "000000"},
  141. {Type: "left", Style: 1, Color: "000000"},
  142. {Type: "right", Style: 1, Color: "000000"},
  143. {Type: "bottom", Style: 1, Color: "000000"},
  144. }
  145. styleLeft, _ := f.NewStyle(&excelize.Style{
  146. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  147. Border: border,
  148. })
  149. b := &LatestExamExcel{
  150. Title: "用户信息",
  151. SheetName: "Sheet1",
  152. Excel: f,
  153. Offset: 0,
  154. AlignCenterStyle: styleLeft,
  155. RowMap: map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6},
  156. RowWidthArray: []float64{16, 12, 12, 10, 10, 12, 16},
  157. RowsHeightArray: make([]map[int]float64, 0),
  158. }
  159. // f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  160. return b
  161. }
  162. func (b *LatestExamExcel) FormatToEmpty(str *string) {
  163. if *str == "0" || *str == "0.000" {
  164. *str = "-"
  165. }
  166. }