こんにちは、utf-8 エンコーディングを使用して mysql db から生成されたクエリセットの csv ファイルを返す単純なビューがあります。
def export_csv(request):
...
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=search_results.csv'
writer = csv.writer(response, dialect=csv.excel)
for item in query_set:
writer.writerow(smart_str(item))
return response
return render(request, 'search_results.html', context)
これは CSV ファイルとして正常に機能し、テキスト エディター、LibreOffice などで問題なく開くことができます。
ただし、Windows の MS Excel でエラーなく開くことができるファイルを提供する必要があります。「Española」などのクエリセットにラテン文字を含む文字列がある場合、Excel の出力は「Española」です。
このブログ投稿を試しましたが、役に立ちませんでした。xlwt パッケージについても知っていますが、現在持っている CSV メソッドを使用して出力を修正する方法があるかどうか知りたいです。
どんな助けでも大歓迎です。