数据采集
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

11 个月前
123456789101112131415161718192021222324252627282930313233343536
  1. package utils
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/360EntSecGroup-Skylar/excelize"
  6. "github.com/gin-gonic/gin"
  7. "io/ioutil"
  8. )
  9. func Output(c *gin.Context, name string, data map[string]string) string {
  10. //创建excel文件
  11. xlsx := excelize.NewFile()
  12. //创建新表单
  13. index := xlsx.NewSheet(name)
  14. for k, v := range data {
  15. //设置单元格的值
  16. xlsx.SetCellValue(name, k, v)
  17. }
  18. //设置默认打开的表单
  19. xlsx.SetActiveSheet(index)
  20. ////保存文件到指定路径
  21. //err := xlsx.SaveAs("./" + name + ".xlsx")
  22. //if err != nil {
  23. // log.Fatal(err)
  24. //}
  25. //_ = file.Save(fileName)
  26. c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name+".xlsx"))
  27. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  28. var buffer bytes.Buffer
  29. _ = xlsx.Write(&buffer)
  30. r := bytes.NewReader(buffer.Bytes())
  31. fSrc, _ := ioutil.ReadAll(r)
  32. str := "data:application/vnd.ms-excel;base64," + Base64RawStdEncode(string(fSrc))
  33. return str
  34. }