django使用xlwt导出excel文件实例代码


Posted in Python onFebruary 06, 2018

本文研究的主要是记录一下下导出的方法,并没有做什么REST处理和异常处理。

维护统一的style样式,可以使导出的数据更加美观。

def export_excel(request): 
  # 设置HttpResponse的类型
  response = HttpResponse(content_type='application/vnd.ms-excel') 
  response['Content-Disposition'] = 'attachment;filename=user.xls' 
  # new一个文件
  wb = xlwt.Workbook(encoding = 'utf-8') 
  # new一个sheet
  sheet = wb.add_sheet(u'人员表单')
  # 维护一些样式, style_heading, style_body, style_red, style_green
 style_heading = xlwt.easyxf("""
    font:
      name Arial,
      colour_index white,
      bold on,
      height 0xA0;
    align:
      wrap off,
      vert center,
      horiz center;
    pattern:
      pattern solid,
      fore-colour 0x19;
    borders:
      left THIN,
      right THIN,
      top THIN,
      bottom THIN;
    """
  )
  style_body = xlwt.easyxf("""
    font:
      name Arial,
      bold off,
      height 0XA0;
    align:
      wrap on,
      vert center,
      horiz left;
    borders:
      left THIN,
      right THIN,
      top THIN,
      bottom THIN;
    """
  )
  style_green = xlwt.easyxf(" pattern: pattern solid,fore-colour 0x11;")
  style_red = xlwt.easyxf(" pattern: pattern solid,fore-colour 0x0A;")
  fmts = [
    'M/D/YY',
    'D-MMM-YY',
    'D-MMM',
    'MMM-YY',
    'h:mm AM/PM',
    'h:mm:ss AM/PM',
    'h:mm',
    'h:mm:ss',
    'M/D/YY h:mm',
    'mm:ss',
    '[h]:mm:ss',
    'mm:ss.0',
  ]
  style_body.num_format_str = fmts[0]

  # 写标题栏
  sheet.write(0,0, '姓名', style_heading) 
  sheet.write(0,1, '英文名', style_heading) 
  sheet.write(0,2, '职位', style_heading) 
  sheet.write(0,3, '公司电话', style_heading) 
  sheet.write(0,4, '手机', style_heading) 
  sheet.write(0,5, 'QQ', style_heading) 
  sheet.write(0,6, 'MSN', style_heading) 
  sheet.write(0,7, 'Email', style_heading) 
  sheet.write(0,8, '办公地点', style_heading) 
  sheet.write(0,9, '部门', style_heading)
  sheet.write(0,10, '人员状态', style_heading)
   
  # 写数据
  row = 1 
  for usa in employesInfo.objects.all():
    sheet.write(row,0, usa.name, style_body)
    sheet.write(row,1, usa.eName, style_body)
    sheet.write(row,2, usa.postion, style_body)
    sheet.write(row,3, usa.cPhone, style_body)
    sheet.write(row,4, usa.pPhone, style_body)
    sheet.write(row,5, usa.qq, style_body)
    sheet.write(row,6, usa.msn, style_body)
    sheet.write(row,7, usa.email, style_body)
    sheet.write(row,8, usa.offAreas, style_body)
    sheet.write(row,9, usa.depart, style_body)
    if int(usa.status) == 1:
      sheet.write(row,10, '在职',style_green)
    else:
      sheet.write(row,10,'离职', style_red)
    row=row + 1 
  
  # 写出到IO
  output = StringIO.StringIO()
  wb.save(output)
  # 重新定位到开始
  output.seek(0)
  response.write(output.getvalue()) 
  return response

总结

以上就是本文关于django使用xlwt导出excel文件实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
python修改注册表终止360进程实例
Oct 13 Python
python3使用QQ邮箱发送邮件
May 20 Python
浅谈python常用程序算法
Mar 22 Python
Python一行代码实现快速排序的方法
Apr 30 Python
Django 使用easy_thumbnails压缩上传的图片方法
Jul 26 Python
Python 中的 import 机制之实现远程导入模块
Oct 29 Python
python3获取文件中url内容并下载代码实例
Dec 27 Python
python中68个内置函数的总结与介绍
Feb 24 Python
什么是Python变量作用域
Jun 03 Python
经验丰富程序员才知道的8种高级Python技巧
Jul 27 Python
python实现文件分片上传的接口自动化
Nov 19 Python
Python爬虫UA伪装爬取的实例讲解
Feb 19 Python
Python使用装饰器进行django开发实例代码
Feb 06 #Python
Python yield与实现方法代码分析
Feb 06 #Python
Django中间件工作流程及写法实例代码
Feb 06 #Python
Django数据库表反向生成实例解析
Feb 06 #Python
Python使用functools实现注解同步方法
Feb 06 #Python
django中send_mail功能实现详解
Feb 06 #Python
Python打印“菱形”星号代码方法
Feb 05 #Python
You might like
mysql4.1以上版本连接时出现Client does not support authentication protocol问题解决办法
2007/03/15 PHP
Laravel 中使用 Vue.js 实现基于 Ajax 的表单提交错误验证操作
2017/06/30 PHP
PHP的mysqli_set_charset()函数讲解
2019/01/23 PHP
php生成短网址/短链接原理和用法实例分析
2020/05/29 PHP
javascript document.compatMode兼容性
2010/02/23 Javascript
js 获取页面高度和宽度兼容 ie firefox chrome等
2014/05/14 Javascript
jQuery插件jPaginate实现无刷新分页
2015/05/04 Javascript
jQuery支持添加事件的日历特效代码分享(3种样式)
2015/08/24 Javascript
如何使用PHP+jQuery+MySQL实现异步加载ECharts地图数据(附源码下载)
2016/02/23 Javascript
Bootstrap导航条可点击和鼠标悬停显示下拉菜单的实现代码
2016/06/23 Javascript
nodejs+express实现文件上传下载管理网站
2017/03/15 NodeJs
微信小程序实战之仿android fragment可滑动底部导航栏(4)
2020/04/16 Javascript
Angular2使用Guard和Resolve进行验证和权限控制
2017/04/24 Javascript
vue ssr 实现方式(学习笔记)
2019/01/18 Javascript
vue.js使用v-model实现表单元素(input) 双向数据绑定功能示例
2019/03/08 Javascript
vue store之状态管理模式的详细介绍
2019/06/13 Javascript
vue 解除鼠标的监听事件的方法
2019/11/13 Javascript
js判断一个对象是数组(函数)的方法实例
2019/12/19 Javascript
Layer UI表格列日期格式化及取消自动填充日期的实现方法
2020/05/10 Javascript
jQuery 淡入/淡出效果函数用法分析
2020/05/19 jQuery
pandas.dataframe中根据条件获取元素所在的位置方法(索引)
2018/06/07 Python
django中账号密码验证登陆功能的实现方法
2019/07/15 Python
python config文件的读写操作示例
2019/09/27 Python
给ubuntu18安装python3.7的详细教程
2020/06/08 Python
Python devel安装失败问题解决方案
2020/06/09 Python
哪种Python框架适合你?简单介绍几种主流Python框架
2020/08/04 Python
python 怎样进行内存管理
2020/11/10 Python
Django用户认证系统如何实现自定义
2020/11/12 Python
纯CSS3实现圆角效果(含IE兼容解决方法)
2014/05/07 HTML / CSS
猫途鹰:全球领先的旅游点评社区
2017/04/07 全球购物
企业消防安全制度
2014/02/02 职场文书
志愿者爱心公益活动策划方案
2014/09/15 职场文书
个人批评与自我批评发言稿
2014/09/28 职场文书
2014年学生会生活部工作总结
2014/11/07 职场文书
西游降魔篇观后感
2015/06/15 职场文书
2019奶茶店创业计划书范本,值得你借鉴
2019/08/14 职场文书