django实现下载数据库中的表导出到excel
view.py:
def download_results_xls(request):
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename="predicted_results.xls"' #attachment --- 作为附件下载
workbook = xlwt.Workbook(encoding='utf-8') #获取该工作薄,编码方式为'utf-8'
worksheet = workbook.add_sheet('predicted_results') #工作表
# 第一页的第一行(列名)
row_num = 0
font_style = xlwt.XFStyle() #初始化样式
font_style.font.bold = True #加粗
columns = ['学号', '姓名', '出勤', '作业', '课堂表现','预测成绩'] #列名
for col_num in range(len(columns)):
worksheet.write(row_num, col_num, columns[col_num], font_style) #写入到第1行的每一列
# 页面内容
font_style = xlwt.XFStyle()
rows = models.T.objects.values_list('N', 'Name', 'Ab', 'Hw','Pm','Rt')
# 写入到第row_num行的每一列
for row in rows:
row_num += 1
for col_num in range(len(row)): #col_num控制列序号
worksheet.write(row_num, col_num, row[col_num], font_style)
workbook.save(response) #保存
return response
html:
<button type="button" class="btn"><a href="{% url 'examapp:导出预测成绩' %}">导出</a></button>
参考:https://www.django.cn/article/show-9.html
Content-Type: application/vnd.ms-excel">
https://www.cnblogs.com/dingjiaoyang/p/5831049.html
如果要将查询结果导出到Excel,只需将页面的Context-Type修改一下就可以了:
header( "Content-Type: application/vnd.ms-excel">
如果希望能够提供那个打开/保存的对话框,Content-Disposition参数,Content-Disposition参数本来是为了在客户端另存文件时提供一个建议的文件名,但是考虑到安全的原因,就从规范中去掉了这个参数
Content-Disposition参数:
attachment --- 作为附件下载
inline --- 在线打开
具体使用如:
header("Content-Disposition: inline; filename=文件名.mp3");
Header("Content-Disposition:attachment;filename=test.xls");
其实IE是根据Content-Disposition中filename这个段中文件名的后缀来识别这个文件类型的,所以,
如果有很多种文件类型的时候,可以将Content-Type设置为二进制模式的:
Header("Content-type: application/octet-stream");
示例:
<?
filename=′./download/d.rar′;filename=′./download/d.rar′;filesize = filesize(filename);header("Content−Type:application/force−download");header("Content−Disposition:attachment;filename=".basename(filename);header("Content−Type:application/force−download");header("Content−Disposition:attachment;filename=".basename(filename));
header( "Content-Length: ".filesize);filesize);data = file_get_contents(filename);echofilename);echodata;
?>
这段代码的意识是打开页面后立即出现下载保存窗口,下载的文件为filename,摘取了常用的部分,其实还有其他一些filename,摘取了常用的部分,其实还有其他一些mimetypes = array(
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'dir' => 'application/x-director',
'js' => 'application/x-javascript',
'swf' => 'application/x-shockwave-flash',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mp3' => 'audio/mpeg',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'wav' => 'audio/x-wav',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'png' => 'image/png',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'txt' => 'text/plain',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
);
django获取某一个字段的列表,values/values_list/flat
https://blog.csdn.net/weixin_42506747/article/details/80772791
class Building(models.Model):
corporation = models.ForeignKey('company.Corporation', verbose_name=u'学校', related_name='buildings')
number = models.CharField(u'楼栋编号', max_length=10, unique=True, db_index=True)
create_time = models.DateTimeField(u'创建时间', auto_now_add=True)
获取Building的number字段列表
In [1]: from apps.dormitory.models import Building
In [2]: buildings = Building.objects.values('number')
In [3]: buildings
Out[3]: [{'number': u'1'}, {'number': u'2'}, {'number': u'3'}, {'number': u'4'}, {'number': u'5'}]
In [4]: buildings_ = Building.objects.values_list('number')
In [5]: buildings_
Out[5]: [(u'1',), (u'2',), (u'3',), (u'4',), (u'5',)]
In [6]: buildings_list = Building.objects.values_list('number', flat=True)
In [7]: buildings_list
Out[7]: [u'1', u'2', u'3', u'4', u'5']
从以上代码可以看出:
values方法可以获取number字段的字典列表。
values_list可以获取number的元组列表。
values_list方法加个参数flat=True可以获取number的值列表。