123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- package api
- import (
- "fmt"
- "strconv"
- "strings"
- "github.com/xuri/excelize/v2"
- )
- type LatestExamExcel struct {
- Offset int
- Row int
- Title string //标题
- Excel *excelize.File
- SheetName string
- AlignCenterStyle int
- Content []*HandleLatestExamLog
- RowMap map[string]int
- RowWidthArray []float64
- RowsHeightArray []map[int]float64
- }
- // 批量设置行高
- func (b *LatestExamExcel) setRowsHeight() {
- for _, rowHeight := range b.RowsHeightArray {
- for row, height := range rowHeight {
- b.Excel.SetRowHeight(b.SheetName, row, height)
- }
- }
- }
- // 获取范围内单元格的宽度 A:F
- func (b *LatestExamExcel) getRangeWidth(r string) float64 {
- rg := strings.Split(r, ":")
- if len(rg) == 1 {
- start := b.RowMap[rg[0]]
- return b.RowWidthArray[start]
- } else if len(rg) == 2 {
- start := b.RowMap[rg[0]]
- end := b.RowMap[rg[1]]
- rowr := b.RowWidthArray[start : end+1]
- width := 0.0
- for _, v := range rowr {
- width += v
- }
- return width
- }
- return 0.0
- }
- func (b *LatestExamExcel) drawTitle() error {
- b.Row++
- startCell := fmt.Sprintf("A%d", b.Row)
- endCell := fmt.Sprintf("G%d", b.Row)
- b.RowMap = map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6}
- b.RowWidthArray = []float64{16, 12, 12, 10, 10, 12, 16}
- b.Excel.SetColWidth(b.SheetName, "A", "A", 16)
- b.Excel.SetColWidth(b.SheetName, "B", "B", 12)
- b.Excel.SetColWidth(b.SheetName, "C", "C", 12)
- b.Excel.SetColWidth(b.SheetName, "D", "D", 10)
- b.Excel.SetColWidth(b.SheetName, "E", "E", 10)
- b.Excel.SetColWidth(b.SheetName, "F", "F", 12)
- b.Excel.SetColWidth(b.SheetName, "G", "G", 16)
- err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
- if err != nil {
- return err
- }
- style, err := b.Excel.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- Font: &excelize.Font{Bold: true, Size: 18}})
- if err != nil {
- return err
- }
- err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
- if err != nil {
- return err
- }
- b.Excel.SetRowHeight(b.SheetName, b.Row, 26)
- b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
- return nil
- }
- func (b *LatestExamExcel) drawTableTitle() error {
- b.Row++
- var drawCol = func(prefix string, value string) error {
- cell := fmt.Sprintf("%s%d", prefix, b.Row)
- err := b.Excel.SetCellStyle(b.SheetName, cell, cell, b.AlignCenterStyle)
- if err != nil {
- return err
- }
- return b.Excel.SetCellValue(b.SheetName, cell, value)
- }
- drawCol("A", "考试日期")
- drawCol("B", "用户名")
- drawCol("C", "编码")
- drawCol("D", "类型")
- drawCol("E", "分数")
- drawCol("F", "学习进度(%)")
- drawCol("G", "学习总时长(分钟)")
- b.Excel.SetRowHeight(b.SheetName, b.Row, 22)
- return nil
- }
- func (b *LatestExamExcel) drawTableContent() error {
- b.Row++
- var DrawRow = func(rowIndex int, values ...string) float64 {
- charas := []string{"A", "B", "C", "D", "E", "F", "G"}
- // 获取该行最大行高
- max := getRowHeight(values[0], b.getRangeWidth(charas[0]))
- for i, c := range charas {
- v := ""
- if i < len(values) {
- v = values[i]
- }
- b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
- val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
- b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
- if getRowHeight(v, b.getRangeWidth(c)) > max {
- max = getRowHeight(v, b.getRangeWidth(c))
- }
- }
- return max
- }
- latestExamLogs := b.Content
- if len(latestExamLogs) > 0 {
- for _, examLog := range latestExamLogs {
- score := strconv.Itoa(examLog.Score)
- completeRate := fmt.Sprintf("%d%%", examLog.CompleteRate)
- learnTime := strconv.Itoa(examLog.LearnTime)
- rowMaxHeight := DrawRow(b.Row, examLog.ExamDate, examLog.UserName, examLog.Nid, examLog.Type, score,
- completeRate, learnTime)
- b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: rowMaxHeight})
- b.Row++
- }
- }
- return nil
- }
- func (b *LatestExamExcel) Draws() {
- b.drawTitle()
- b.drawTableTitle()
- b.drawTableContent()
- // 设置行高
- b.setRowsHeight()
- }
- func NewLatestExamExcel(f *excelize.File) *LatestExamExcel {
- border := []excelize.Border{
- {Type: "top", Style: 1, Color: "000000"},
- {Type: "left", Style: 1, Color: "000000"},
- {Type: "right", Style: 1, Color: "000000"},
- {Type: "bottom", Style: 1, Color: "000000"},
- }
- styleLeft, _ := f.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: border,
- })
- b := &LatestExamExcel{
- Title: "用户信息",
- SheetName: "Sheet1",
- Excel: f,
- Offset: 0,
- AlignCenterStyle: styleLeft,
- RowMap: map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6},
- RowWidthArray: []float64{16, 12, 12, 10, 10, 12, 16},
- RowsHeightArray: make([]map[int]float64, 0),
- }
- // f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
- return b
- }
- func (b *LatestExamExcel) FormatToEmpty(str *string) {
- if *str == "0" || *str == "0.000" {
- *str = "-"
- }
- }
|