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版微信红包分配算法
May 04 Python
解决Python requests 报错方法集锦
Mar 19 Python
Python爬虫实例爬取网站搞笑段子
Nov 08 Python
Python实现统计给定字符串中重复模式最高子串功能示例
May 16 Python
在python 不同时区之间的差值与转换方法
Jan 14 Python
Python3简单实现串口通信的方法
Jun 12 Python
如何通过雪花算法用Python实现一个简单的发号器
Jul 03 Python
python集合的创建、添加及删除操作示例
Oct 08 Python
Python 中如何实现参数化测试的方法示例
Dec 10 Python
python中sympy库求常微分方程的用法
Apr 28 Python
PO模式在selenium自动化测试框架的优势
Mar 20 Python
Python中time与datetime模块使用方法详解
Mar 31 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
备份mysql数据库的php代码(一个表一个文件)
2010/05/28 PHP
PHP高自定义性安全验证码代码
2011/11/27 PHP
修改apache配置文件去除thinkphp url中的index.php
2014/01/17 PHP
在win7中搭建Linux+PHP 开发环境
2014/10/08 PHP
PHP加密解密函数详解
2015/10/28 PHP
php中10个不同等级压缩优化图片操作示例
2016/11/14 PHP
ThinkPHP5.1框架页面跳转及修改跳转页面模版示例
2019/05/06 PHP
js下用层来实现select的title提示属性
2010/02/23 Javascript
ExtJS4 组件化编程,动态加载,面向对象,Direct
2011/05/12 Javascript
javascript setTimeout和setInterval计时的区别详解
2013/06/21 Javascript
javascript 获取HTML DOM父、子、临近节点
2014/06/16 Javascript
jquery实现最简单的滑动菜单效果代码
2015/09/12 Javascript
js+html5操作sqlite数据库的方法
2016/02/02 Javascript
分析JS中this引发的bug
2017/12/12 Javascript
深入浅析JSONAPI在PHP中的应用
2017/12/24 Javascript
nodejs微信开发之授权登录+获取用户信息
2019/03/17 NodeJs
vue项目中mock.js的使用及基本用法
2019/05/22 Javascript
[55:18]Liquid vs Chaos 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
python实现的解析crontab配置文件代码
2014/06/30 Python
Python性能优化技巧
2015/03/09 Python
Python爬虫包BeautifulSoup简介与安装(一)
2018/06/17 Python
Python 变量类型详解
2018/10/10 Python
Scrapy使用的基本流程与实例讲解
2018/10/21 Python
Python 爬虫实现增加播客访问量的方法实现
2019/10/31 Python
python爬虫开发之selenium模块详细使用方法与实例全解
2020/03/09 Python
可视化pytorch 模型中不同BN层的running mean曲线实例
2020/06/24 Python
Django自带用户认证系统使用方法解析
2020/11/12 Python
StubHub智利:购买和出售您的门票
2016/11/23 全球购物
法国房车租赁网站:Yescapa
2019/08/26 全球购物
乌克兰巴士票购买网站:inBus
2021/03/12 全球购物
什么是Remote Module
2016/06/10 面试题
如何用Python来进行查询和替换一个文本字符串
2014/01/02 面试题
大学应届生求职简历的自我评价
2013/10/08 职场文书
植树造林的宣传标语
2014/06/23 职场文书
2014年外贸业务员工作总结
2014/12/11 职场文书
Ajax实现局部刷新的方法实例
2021/03/31 Javascript