django 生成csv excel 文件名及文件内容 中文乱码 解决

#attachment表示以文件形式下载,urlquote解决文件名中文文字乱码解决
#codecs.BMO_UTF8解决文件内容中文乱码
from app1 import models
from django.shortcuts import render,redirect,HttpResponse
import csv,codecs
from django.utils.http import urlquote

def downloadSopCsv(request):
    sops = models.StandardFile.objects.all().order_by("-cusDate").values_list("title","cus__companyName","cusDate","qs__name","files__name")
    response = HttpResponse(content_type="text/csv")
    #attachment表示以文件形式下载,urlquote解决文件名中文文字乱码解决
    #codecs.BMO_UTF8解决文件内容中文乱码
    response.write(codecs.BOM_UTF8)
    response['Content-Disposition'] = 'attachment;filename="%s"'%(urlquote("raylu.csv"))
    writer = csv.writer(response)
    writer.writerow(["filename","customerCompany","date","createMember","attchmentFile"])
    for sop in sops:
        writer.writerow(sop)
    return response

猜你喜欢

转载自blog.csdn.net/java_raylu/article/details/84558096