数据采集
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xls.go 994 B

10 months ago
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. }