附近小店
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.
 
 
 

80 regels
3.0 KiB

  1. package utils
  2. import (
  3. "applet/app/db/model"
  4. "bytes"
  5. "encoding/csv"
  6. "fmt"
  7. "github.com/360EntSecGroup-Skylar/excelize"
  8. "github.com/gin-gonic/gin"
  9. "io/ioutil"
  10. )
  11. func Output(c *gin.Context, name string, data map[string]string) string {
  12. //创建excel文件
  13. xlsx := excelize.NewFile()
  14. //创建新表单
  15. index := xlsx.NewSheet(name)
  16. for k, v := range data {
  17. //设置单元格的值
  18. xlsx.SetCellValue(name, k, v)
  19. }
  20. //设置默认打开的表单
  21. xlsx.SetActiveSheet(index)
  22. ////保存文件到指定路径
  23. //err := xlsx.SaveAs("./" + name + ".xlsx")
  24. //if err != nil {
  25. // log.Fatal(err)
  26. //}
  27. //_ = file.Save(fileName)
  28. c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name+".xlsx"))
  29. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  30. var buffer bytes.Buffer
  31. _ = xlsx.Write(&buffer)
  32. r := bytes.NewReader(buffer.Bytes())
  33. fSrc, _ := ioutil.ReadAll(r)
  34. str := "data:application/vnd.ms-excel;base64," + Base64RawStdEncode(string(fSrc))
  35. return str
  36. }
  37. func OutputSecond(c *gin.Context, name string, data *[]model.CommunityTeamStoreWithdrawApply) string {
  38. bytesBuffer := &bytes.Buffer{}
  39. _, _ = bytesBuffer.WriteString("\xEF\xBB\xBF") //写入UTF-8 BOM 防止乱码
  40. writer := csv.NewWriter(bytesBuffer)
  41. writer.Write([]string{"支付宝批量付款文件模板"})
  42. writer.Write([]string{"序号(必填)", "收款方支付宝账号(必填)", "收款方姓名(必填)", "金额(必填,单位:元)", "备注(选填)"})
  43. if data != nil {
  44. for _, v := range *data {
  45. writer.Write([]string{IntToStr(v.Id), v.WithdrawAccount, v.WithdrawName, v.Amount, "门店提现"})
  46. }
  47. }
  48. writer.Flush() // 此时才会将缓冲区数据写入
  49. c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name+".csv"))
  50. c.Header("Content-Type", "application/force-download")
  51. c.Header("Accept-Ranges", "bytes")
  52. r := bytes.NewReader(bytesBuffer.Bytes())
  53. fSrc, _ := ioutil.ReadAll(r)
  54. str := "data:text/csv;charset=utf-8;base64," + Base64RawStdEncode(string(fSrc))
  55. fmt.Println(str)
  56. return str
  57. }
  58. //func OutputSecond(c *gin.Context, name string, data []map[string]string) string {
  59. // bytesBuffer := &bytes.Buffer{}
  60. // _, _ = bytesBuffer.WriteString("\xEF\xBB\xBF") //写入UTF-8 BOM 防止乱码
  61. // writer := csv.NewWriter(bytesBuffer)
  62. // writer.Write([]string{"支付宝批量付款文件模板"})
  63. // writer.Write([]string{"序号(必填)", "收款方支付宝账号(必填)", "收款方姓名(必填)", "金额(必填,单位:元)", "备注(选填)"})
  64. // for _, v := range data {
  65. // writer.Write([]string{v["id"], v["alipay"], v["alipay_name"], v["real_amount"], "聚合联盟"})
  66. // }
  67. // writer.Flush() // 此时才会将缓冲区数据写入
  68. // c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name+".csv"))
  69. // r := bytes.NewReader(bytesBuffer.Bytes())
  70. // fSrc, _ := ioutil.ReadAll(r)
  71. // str := "data:text/csv;charset=utf-8;base64," + Base64RawStdEncode(string(fSrc))
  72. // fmt.Println(str)
  73. // return str
  74. //}