Golang导出csv

tech2023-02-07  114

package main import ( "encoding/csv" "os" "strconv" ) type Demo struct { SpuId int64 `json:"spu_id"` Title string `json:"title"` } func main() { var data Demo //创建一个文件 f, err := os.Create("C:\\Users\\Deng\\Desktop\\xx.csv") if err != nil { panic(err) } defer f.Close() //写入UTF-8 BOM,避免使用Microsoft Excel打开乱码 _, _ = f.WriteString("\xEF\xBB\xBF") writer := csv.NewWriter(f) //表头 _ = writer.Write([]string{"spuId", "商品名称"}) _ = writer.Write([]string{ strconv.FormatInt(data.SpuId, 10) + "\t", data.Title, }) //将缓冲区数据写入 writer.Flush() }
最新回复(0)