package utils import ( "applet/app/db/model" "bytes" "encoding/csv" "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 } func OutputSecond(c *gin.Context, name string, data *[]model.CommunityTeamStoreWithdrawApply) string { bytesBuffer := &bytes.Buffer{} _, _ = bytesBuffer.WriteString("\xEF\xBB\xBF") //写入UTF-8 BOM 防止乱码 writer := csv.NewWriter(bytesBuffer) writer.Write([]string{"支付宝批量付款文件模板"}) writer.Write([]string{"序号(必填)", "收款方支付宝账号(必填)", "收款方姓名(必填)", "金额(必填,单位:元)", "备注(选填)"}) if data != nil { for _, v := range *data { writer.Write([]string{IntToStr(v.Id), v.WithdrawAccount, v.WithdrawName, v.Amount, "门店提现"}) } } writer.Flush() // 此时才会将缓冲区数据写入 c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name+".csv")) c.Header("Content-Type", "application/force-download") c.Header("Accept-Ranges", "bytes") r := bytes.NewReader(bytesBuffer.Bytes()) fSrc, _ := ioutil.ReadAll(r) str := "data:text/csv;charset=utf-8;base64," + Base64RawStdEncode(string(fSrc)) fmt.Println(str) return str } //func OutputSecond(c *gin.Context, name string, data []map[string]string) string { // bytesBuffer := &bytes.Buffer{} // _, _ = bytesBuffer.WriteString("\xEF\xBB\xBF") //写入UTF-8 BOM 防止乱码 // writer := csv.NewWriter(bytesBuffer) // writer.Write([]string{"支付宝批量付款文件模板"}) // writer.Write([]string{"序号(必填)", "收款方支付宝账号(必填)", "收款方姓名(必填)", "金额(必填,单位:元)", "备注(选填)"}) // for _, v := range data { // writer.Write([]string{v["id"], v["alipay"], v["alipay_name"], v["real_amount"], "聚合联盟"}) // } // writer.Flush() // 此时才会将缓冲区数据写入 // c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name+".csv")) // r := bytes.NewReader(bytesBuffer.Bytes()) // fSrc, _ := ioutil.ReadAll(r) // str := "data:text/csv;charset=utf-8;base64," + Base64RawStdEncode(string(fSrc)) // fmt.Println(str) // return str //}