Golang export csv garbled solution:
Write UTF-8 BOM at the beginning of the csv file
// 创建文件 dstf, err := os.Create("./data/" + fileName) defer dstf.Close() if err != nil { beego.Error("create file error: " + err.Error()) return "", err } dstf.WriteString("\xEF\xBB\xBF") // 写入UTF-8 BOM,防止中文乱码 // 写数据到文件 w := csv.NewWriter(dstf) w.Write([]string{"开始时间:", model.StartTime}) w.Write([]string{"结束时间:", model.EndTime}) w.Write([]string{"编号", "优惠卷名称", "优惠卷编号", "优惠卷类型", "优惠卷面值", "核销日期"})
WriteString:
WriteString["file","string"]
Write "string" to the file.
WriteString[channel,"string"]
Write "string" Enter the stream or process.
WriteString[channel,expr1,expr2,…]
Convert expri into strings, and then write them to the specified output channel in sequence.
For more golang knowledge, please pay attention to the golang tutorial column.
The above is the detailed content of Golang export csv garbled solution. For more information, please follow other related articles on the PHP Chinese website!