package excel import ( "github.com/360EntSecGroup-Skylar/excelize/v2" "strconv" ) var Excel = &excelLib{} type excelLib struct { } type Row struct { Value string ColSpan uint RowSpan uint } // SetRow @Title 设置列数据 func (e *excelLib) SetRow(file *excelize.File, sheet string, rowIndex int, row []Row) { colIndex := 0 for index, data := range row { celName := e.excelColName(index+colIndex, rowIndex) newRowIndex := rowIndex if data.ColSpan > 1 { colIndex += int(data.ColSpan) - 1 } if data.RowSpan > 1 { newRowIndex += int(data.RowSpan) - 1 } mergeName := e.excelColName(index+colIndex, newRowIndex) if celName != mergeName { file.MergeCell(sheet, celName, mergeName) } file.SetCellValue(sheet, celName, data.Value) } } func (e *excelLib) excelColName(colIndex, rowIndex int) (result string) { return e.excelCol(uint(colIndex)) + strconv.Itoa(rowIndex) } func (e *excelLib) excelCol(colIndex uint) (result string) { for { if result != "" { colIndex-- } result = string(rune(65+colIndex%26)) + result colIndex /= 26 if colIndex == 0 { break } } return }