English 中文(简体)
Django csv Excel 的输出
原标题:Django csv output in Excel

Hi 我有一个简单的视图, 它返回一个 Csv 文件, 查询集是由使用 utf-8 编码的 Mysql db 生成的 :

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 打开的文件。 如果我在查询中与 latin 字符如 Española 连接, 那么Excel 的输出就是 Espaçãola 。

我尝试了这个 < a href=>" "http://crashcoursing.blogspot.com/" rel="nofollow">>blogpoint ,但是它没有帮助。我也知道这个" http://pypi.python.org/pypi/xlwt" rel="nofollow">xlwt passage ,但我好奇,如果有什么方法可以纠正输出, 使用我目前拥有的 CSV 方法。

任何帮助 非常感谢。

问题回答

看来所有Excel版本都没有统一的解决办法。

  1. Your best bet migth be to go with openpyxl, but this is rather complicated and requiers separate handling of downloads for excel users which is not optimal.

  2. 尝试在文件首端添加字节顺序标记( 0xEF, 0xBB, 0xBF) 。 见 < a href=" https://stackoverflow.com/ queses/ 155097/microsoft- excel- mangles- diacritics- in- csv- files > 微软- excel- mangles- diacritics- in- csv- files

还有另一个"https://stackoverflow.com/ questions/6002256/is-it-possible-to-force-excel-recognize-utf-8-csv-files-autally"。>类似的文章

您可以给 python- unicodcsv a go。 它替换了不优雅地处理 Unicode 的 python < code> < code> csv 模块 。

  1. Put the unicodecsv folder somehwere you can import it or install via setup.py
  2. 将它导入到查看文件, 如 :

    import unicodecsv as csv
    

我发现Excel有三件事要做, 才能正确打开 Unicode Csv 文件:

  1. Use utf-16-le charset
  2. Insert utf-16 byte order mark to the beginning of exported file
  3. Use tabs instead of commas in csv

因此,这应该使它在Python 3.7和Django 2.2中发挥作用。

import codecs
...

def export_csv(request):
    ...
    response = HttpResponse(content_type= text/csv , charset= utf-16-le )
    response[ Content-Disposition ] =  attachment; filename=search_results.csv 
    response.write(codecs.BOM_UTF16_LE)

    writer = csv.writer(response, dialect= excel-tab )
    for item in query_set:
        writer.writerow(smart_str(item))
    return response




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签