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发送HTTP请求的方法小结
Jul 08 Python
在Django中管理Users和Permissions以及Groups的方法
Jul 23 Python
基于Python List的赋值方法
Jun 23 Python
浅谈Python在pycharm中的调试(debug)
Nov 29 Python
python修改txt文件中的某一项方法
Dec 29 Python
Python字符串逆序输出的实例讲解
Feb 16 Python
Python 线程池用法简单示例
Oct 02 Python
Python二元赋值实用技巧解析
Oct 25 Python
如何解决tensorflow恢复模型的特定值时出错
Feb 06 Python
Python爬虫爬取百度搜索内容代码实例
Jun 05 Python
浅析Python 中的 WSGI 接口和 WSGI 服务的运行
Dec 09 Python
python中封包建立过程实例
Feb 18 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
PHP中json_encode、json_decode与serialize、unserialize的性能测试分析
2010/06/09 PHP
php汉字转拼音的示例
2014/02/27 PHP
与jquery serializeArray()一起使用的函数,主要来方便提交表单
2011/01/31 Javascript
关于html+ashx开发中几个问题的解决方法
2011/07/18 Javascript
jquery三个关闭弹出层的小示例
2013/11/05 Javascript
javascript通过元素id和name直接取得元素的方法
2015/04/28 Javascript
Backbone.js的Hello World程序实例
2015/06/19 Javascript
easyui Draggable组件实现拖动效果
2015/08/19 Javascript
分享javascript实现的冒泡排序代码并优化
2016/06/05 Javascript
分享JS代码实现鼠标放在输入框上输入框和图片同时更换样式
2016/09/01 Javascript
详解AngularJS用Interceptors来统一处理HTTP请求和响应
2017/06/08 Javascript
深入研究React中setState源码
2017/11/17 Javascript
js中getBoundingClientRect的作用及兼容方案详解
2018/02/01 Javascript
详解React中传入组件的props改变时更新组件的几种实现方法
2018/09/13 Javascript
Nodejs把接收图片base64格式保存为文件存储到服务器上
2018/09/26 NodeJs
python检查指定文件是否存在的方法
2015/07/06 Python
Python向日志输出中添加上下文信息
2017/05/24 Python
Python生成8位随机字符串的方法分析
2017/12/05 Python
Centos7 Python3下安装scrapy的详细步骤
2018/03/15 Python
Python Pandas找到缺失值的位置方法
2018/04/12 Python
Python函数中不定长参数的写法
2019/02/13 Python
python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法
2020/02/14 Python
python七种方法判断字符串是否包含子串
2020/08/18 Python
肯尼亚网上商城:Kilimall
2016/08/20 全球购物
英国安全产品购物网站:The Safe Shop
2017/03/20 全球购物
美国领先的奢侈美容零售商:Bluemercury
2017/07/26 全球购物
英国珠宝钟表和家居礼品精品店:David Shuttle
2018/02/24 全球购物
实现向右循环移位
2014/07/31 面试题
测试工程师岗位职责
2013/11/28 职场文书
金融行业务员的自我评价
2013/12/13 职场文书
校长寄语大全
2014/04/09 职场文书
MySQL查看表和清空表的常用命令总结
2021/05/26 MySQL
Win10 heic文件怎么打开 ? Win10 heic文件打开教程
2022/04/06 数码科技
Golang 入门 之url 包
2022/05/04 Golang
Java 中的 Lambda List 转 Map 的多种方法详解
2022/07/07 Java/Android
TypeScript 内置高级类型编程示例
2022/09/23 Javascript