🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
> ### 导出CSV表格 ~~~ package main import ( "encoding/csv" "fmt" "github.com/gin-gonic/gin" "os" ) func DownloadCSV(c *gin.Context, fileName string, tableHeader []string, tableData [][]string) error { newFile, err := os.Create("./logs/" + fileName) if err != nil { return err } defer func() { newFile.Close() }() newFile.WriteString("\xEF\xBB\xBF") // 写入UTF-8 BOM,防止中文乱码 w := csv.NewWriter(newFile) data := [][]string{tableHeader,} for _, v := range tableData { data = append(data, v) } w.WriteAll(data) w.Flush() c.Writer.Header().Set("Content-Type", "text/csv") c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=%s", fileName)) c.File("./logs/" + fileName) return err } ~~~