package excel import ( "fmt" "github.com/gin-gonic/gin" "github.com/xuri/excelize/v2" "net/http" "strconv" ) func NewFile() *excelize.File { f := excelize.NewFile() defer func() { if err := f.Close(); err != nil { fmt.Println(err) } }() return f } func Download(f *excelize.File, fileName string, c *gin.Context) { c.Header("Content-Disposition", "attachment; filename="+fileName) c.Header("Content-Type", "application/vnd.ms-excel") c.Status(http.StatusOK) err := f.Write(c.Writer) if err != nil { fmt.Println(err.Error()) } } func SetSimpleHeaders(headers []string, sheet string, f *excelize.File) { for i, header := range headers { f.SetCellValue(sheet, ColKey(i)+"1", header) } } func ColKey(i int) string { rowKeys := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} return rowKeys[i] } func CellKey(row int, col *int) string { cellKey := ColKey(*col) + strconv.Itoa(row) *col++ return cellKey }