CSVExcelCSVUTF-8无BOM编码格式UTF-8编码格式BOMBOM

PHP

    private function _write_csv($data)
    {
        file_put_contents($this->_file_dir, '');

        $file = fopen($this->_file_dir, 'w');
        fwrite($file, chr(0xEF).chr(0xBB).chr(0xBF)); // 添加 BOM
        $content = [
            '日期',
            '品类',
            '价格',
            '单位',
        ];
        fputcsv($file, $content);
        foreach ($data as $item) {
            $content = [
                $item['product_name'],
                date('Y-m-d', $item['day_time']),
                $item['price'],
                $item['unit_name']
            ];
            fputcsv($file, $content);
        }
        fclose($file);
    }

GoLang

	filePath := "/home/users/weihaoyu/a.csv"
	f, err := os.Create(filePath)
	if err != nil {
		log.Println(err.Error())
		os.Exit(1)
	}
	defer f.Close()
	f.WriteString("\xEF\xBB\xBF")

	writer := csv.NewWriter(f)
	writer.Write([]string{"患者姓名", "患者电话", "医生姓名", "工号", "网络二级科室", "线下二级科室", "订单ID", "患者创建时间", "问诊状态", "问诊开始时间", "结束时间"})
	for _, v := range result {
		writer.Write([]string{v.PatientName, v.PatientMobile, v.DoctorName, v.DoctorCode, v.DeptNameOnline, v.DeptName, v.OrderCode, v.CreateTime, v.Status, v.StartTime, v.EndTime})

		for _, w := range v.Message {
			writer.Write([]string{w.UserName, fmt.Sprintf("%v\n", w.Content)})
		}
	}
	writer.Flush()