django admin 导出excel,csv

来源:互联网 发布:宝鸡国企再造新矩阵 编辑:程序博客网 时间:2024/06/04 19:22

Export Data to CSV File

Easiest way. No need to install dependencies, which is a great thing. Use it if you do not need any fancy formating.

views.py

import csvfrom django.http import HttpResponsefrom django.contrib.auth.models import Userdef export_users_csv(request):    response = HttpResponse(content_type='text/csv')    response['Content-Disposition'] = 'attachment; filename="users.csv"'    writer = csv.writer(response)    writer.writerow(['Username', 'First name', 'Last name', 'Email address'])    users = User.objects.all().values_list('username', 'first_name', 'last_name', 'email')    for user in users:        writer.writerow(user)    return response

urls.py

import viewsurlpatterns = [    ...    url(r'^export/csv/$', views.export_users_csv, name='export_users_csv'),]

template.html

<a href="{% url 'export_users_csv' %}">Export all users</a>

In the example above I used the values_list in the QuerySet for two reasons: First to query only the fields I needed to export, and second because it will return the data in a tuple instead of model instances. The writerow method expects a list/tuple.

Another way to do it would be:

writer.writerow([user.username, user.first_name, user.last_name, user.email, ])

Export Data to XLS File

Use it if you really need to export to a .xls file. You will be able to add formating as bold font, font size, define column size, etc.

First of all, install the xlwt module. The easiest way is to use pip.

pip install xlwt

views.py

import xlwtfrom django.http import HttpResponsefrom django.contrib.auth.models import Userdef export_users_xls(request):    response = HttpResponse(content_type='application/ms-excel')    response['Content-Disposition'] = 'attachment; filename="users.xls"'    wb = xlwt.Workbook(encoding='utf-8')    ws = wb.add_sheet('Users')    # Sheet header, first row    row_num = 0    font_style = xlwt.XFStyle()    font_style.font.bold = True    columns = ['Username', 'First name', 'Last name', 'Email address', ]    for col_num in range(len(columns)):        ws.write(row_num, col_num, columns[col_num], font_style)    # Sheet body, remaining rows    font_style = xlwt.XFStyle()    rows = User.objects.all().values_list('username', 'first_name', 'last_name', 'email')    for row in rows:        row_num += 1        for col_num in range(len(row)):            ws.write(row_num, col_num, row[col_num], font_style)    wb.save(response)    return response

urls.py

import viewsurlpatterns = [    ...    url(r'^export/xls/$', views.export_users_xls, name='export_users_xls'),]

template.html

<a href="{% url 'export_users_xls' %}">Export all users</a>
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 怀孕了肾结石痛怎么办 肾结石痛得厉害怎么办 怀孕了有结石怎么办 孕期尿结石疼痛怎么办 怀孕有结石痛怎么办 肾结石痛又怀孕怎么办 怀孕肾结石疼怎么办啊 孕晚期肾绞痛怎么办 肾绞疼引起的呕吐怎么办 肾绞痛肚子胀气怎么办 iga肾炎肉眼血尿怎么办 结石疼怎么办怎么缓解 肾有问题严重怎么办 肾结石突然很疼怎么办 输尿管结石肉眼血尿该怎么办 结石引起肾绞痛怎么办 尿结石支架后尿里老有血怎么办? 结石堵在输尿管怎么办 尿路结石痛怎么办 怀孕了有肾结石怎么办 怀孕有肾结石怎么办啊 肾结石无疼血尿怎么办 胆囊胆管都结石怎么办 肾里面有肿瘤怎么办 肾癌手术后发烧怎么办 尿结石堵住尿道怎么办 尿结石不能排尿怎么办 肾癌小便有血怎么办 膀胱癌膀胱全切怎么办 怀孕了有阑尾炎怎么办 食物堵塞在食管怎么办 食物卡在食管怎么办 小孩食道卡异物怎么办 八十岁老人得了膀胱癌怎么办 肾结石引起吐血尿血怎么办 肾结石引起的尿血怎么办 食道感觉有异物怎么办 膀胱出血有血块怎么办 肾小球滤过率20怎么办 膀胱癌术后有血尿怎么办 肾病贫血怎么办吃什么