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
- django admin 导出excel,csv
- 导出CSV EXCEL
- excel导出csv
- csv 导出Excel
- 导出excel(csv)
- Django 导出excel
- django excel 导出
- C#实现 EXCEL CSV导出
- c#导出csv、excel格式文件
- asp.net 导出csv,excel
- node.js导出excel ,csv
- PHP 导出csv格式excel
- django导出csv文件的编码问题
- django 操作 下载 excel xls xlsx csv
- Django 导出 Excel 代码详解
- java导出excel,导入excel,导出csv工具类整理
- Django Admin
- django admin
- Listview 随手记
- 2017年-Intel BIOS 工程师面试
- caffe,运行fast-rcnn出现Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer type: ROIPooling
- 欢迎使用CSDN-markdown编辑器
- 419. Battleships in a Board
- django admin 导出excel,csv
- Zookeeper简介
- 解决Ubuntu Chrome Adobe过期的问题
- Qt中的sizeHint,minimumSizeHint,sizePolicy属性
- spark几万个分区表无法执行
- C++ 数据结构链表的实现代码
- Luogu P2799 国王的魔镜
- 【matlab】global的用法
- OpenGL学习总结(四)