package utils import ( "bytes" "fmt" "github.com/360EntSecGroup-Skylar/excelize" "github.com/gin-gonic/gin" "io/ioutil" ) func Output(c *gin.Context, name string, data map[string]string) string { //创建excel文件 xlsx := excelize.NewFile() //创建新表单 index := xlsx.NewSheet(name) for k, v := range data { //设置单元格的值 xlsx.SetCellValue(name, k, v) } //设置默认打开的表单 xlsx.SetActiveSheet(index) ////保存文件到指定路径 //err := xlsx.SaveAs("./" + name + ".xlsx") //if err != nil { // log.Fatal(err) //} //_ = file.Save(fileName) c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name+".xlsx")) c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") var buffer bytes.Buffer _ = xlsx.Write(&buffer) r := bytes.NewReader(buffer.Bytes()) fSrc, _ := ioutil.ReadAll(r) str := "data:application/vnd.ms-excel;base64," + Base64RawStdEncode(string(fSrc)) return str }