Golang export CSV data and solve the problem of data garbled

file

In daily development, for data export, we can export to Excel format, but if it is for a large amount of data export, direct export to Excel format may take up a lot of memory and the export speed is very slow. At this time, we need to export to CSV format.

CSV format

CSV is essentially a text file, which has the following requirements:

  • Use commas to separate columns and newlines to separate rows

  • If the cell contains characters such as commas and quotation marks, the cell needs to be enclosed in double quotation marks

  • If the content contains Chinese, the direct output may be garbled

Method to realize

Golang officially has a csv library, which can easily write csv data.

Golang realizes csv data writing file

func main() {
    f, err := os.Create("data.csv")
    if err != nil {
        panic(err)
    }
    defer f.Close()

    f.WriteString("xEFxBBxBF") // 写入UTF-8 BOM,避免使用Microsoft Excel打开乱码 
    writer := csv.NewWriter(f)
    writer.Write([]string{"编号", "姓名", "年龄"})
    writer.Write([]string{"1", "张三", "23"})
    writer.Write([]string{"2", "李四", "24"})
    writer.Write([]string{"3", "王五", "25"})
    writer.Write([]string{"4", "赵六", "26"})
    writer.Flush() // 此时才会将缓冲区数据写入 }

Golang implements web export csv data

Taking the gin framework as an example, if you use the official go web library, it is actually almost the same:

func ExportCsv(c *gin.Context) {
    bytesBuffer := &bytes.Buffer{}
    bytesBuffer.WriteString("xEFxBBxBF") // 写入UTF-8 BOM,避免使用Microsoft Excel打开乱码 
    writer := csv.NewWriter(bytesBuffer)
    writer.Write([]string{"编号", "姓名", "年龄"})
    writer.Write([]string{"1", "张三", "23"})
    writer.Write([]string{"2", "李四", "24"})
    writer.Write([]string{"3", "王五", "25"})
    writer.Write([]string{"4", "赵六", "26"})

    writer.Flush() // 此时才会将缓冲区数据写入 
    // 设置下载的文件名     c.Writer.Header().Set("Content-Disposition", "attachment;filename=data.csv")
    // 设置文件类型以及输出数据     c.Data(http.StatusOK, "text/csv", bytesBuffer.Bytes())
    return
}

Author: Kotani xg | Source: SegmentFault point here: 2020Python paying large collection of real learning **

[Take it away! Python 3.9 official Chinese document, limited time! ] ( http://dwz.date/dE6v )

[Limited time! Quick collar! 14 high-definition Python cheat sheets, essential for efficiency improvement! ] ( http://dwz.date/dE6w )

[GitHub Biaoxing 3W+, 80 Python cases, let you learn Python easily! ] ( http://dwz.date/dE64 )

Guess you like

Origin blog.csdn.net/weixin_43507410/article/details/112650606