1 Star 0 Fork 0

PeterPZhang / python常用函数demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
根据列表导出为excel表格.py 2.19 KB
一键复制 编辑 原始数据 按行查看 历史
PeterPZhang 提交于 2019-06-21 16:04 . 根据列表导出为Excel表格
# -*- coding: utf-8 -*-
"""
__author__ = 'peter'
__mtime__ = '2019-06-21'
# Follow the master,become a master.
             ┏┓      ┏┓
            ┏┛┻━━━━━━━┛┻┓
            ┃    ☃    ┃
            ┃  ┳┛  ┗┳  ┃
            ┃     ┻    ┃
            ┗━┓      ┏━┛
              ┃      ┗━━━━┓
              ┃ 神兽保佑   ┣┓
              ┃ 永无BUG! ┏┛
              ┗┓┓┏━━━┳┓┏━━━┛
               ┃┫┫  ┃┫┫
               ┗┻┛  ┗┻┛
"""
from io import BytesIO
import xlwt
from django.http import HttpResponse
def pub_export(header_list=None, export_list=None, excel_name='导出表格'):
"""
公用表格导出
:param header_list: 表头,不传则取列表中keys作为表头
:param export_list: 导出列表数据
:param excel_name: 导出表名
:return:
"""
if export_list:
keys = list(export_list[0].keys())
response = HttpResponse(content_type='application/vnd.ms-excel') # 指定返回为excel文件
response['Content-Disposition'] = 'attachment;filename={excel_name}.xls'.format(
excel_name=excel_name) # 指定返回文件名
wb = xlwt.Workbook(encoding='utf-8') # 设定编码类型为utf8
sheet = wb.add_sheet(u'类别') # excel里添加类别
col = 0
if header_list:
for header in header_list:
sheet.write(0, col, header)
col += 1
row = 1
else:
for key in keys:
sheet.write(0, col, key)
col += 1
row = 1
for one_row in export_list:
col = 0
for key in keys:
sheet.write(row, col, one_row[key])
col += 1
row = row + 1
output = BytesIO()
wb.save(output)
output.seek(0)
response.write(output.getvalue())
return response
else:
return
Python
1
https://gitee.com/PeterPZ/python_common_function_demo.git
git@gitee.com:PeterPZ/python_common_function_demo.git
PeterPZ
python_common_function_demo
python常用函数demo
master

搜索帮助