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中处理字符串的相关的len()方法的使用简介
May 19 Python
Python3实现将文件归档到zip文件及从zip文件中读取数据的方法
May 22 Python
python使用urllib2提交http post请求的方法
May 26 Python
Python使用爬虫猜密码
Feb 19 Python
Python+OpenCV人脸检测原理及示例详解
Oct 19 Python
python+matplotlib实现礼盒柱状图实例代码
Jan 16 Python
Python实现获取邮箱内容并解析的方法示例
Jun 16 Python
python实现名片管理系统项目
Apr 26 Python
numpy.linspace函数具体使用详解
May 27 Python
python绘制直方图和密度图的实例
Jul 08 Python
django用户登录验证的完整示例代码
Jul 21 Python
Pymysql实现往表中插入数据过程解析
Jun 02 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中最简单的字符串匹配算法
2014/12/16 PHP
ASP.NET jQuery 实例12 通过使用jQuery validation插件简单实现用户注册页面验证功能
2012/02/03 Javascript
JQuery for与each性能比较分析
2013/05/14 Javascript
js文件缓存之版本管理详解
2013/07/05 Javascript
jquery选择器之属性过滤选择器详解
2014/01/27 Javascript
javascript上下方向键控制表格行选中并高亮显示的方法
2015/02/13 Javascript
Nodejs下DNS缓存问题浅析
2016/11/16 NodeJs
微信小程序实时聊天WebSocket
2018/07/05 Javascript
Nodejs调用Dll模块的方法
2018/09/17 NodeJs
electron-vue利用webpack打包实现多页面的入口文件问题
2019/05/12 Javascript
JS中实现一个下载进度条及播放进度条的代码
2019/06/10 Javascript
Vue.use()在new Vue() 之前使用的原因浅析
2019/08/26 Javascript
新手入门js闭包学习过程解析
2019/10/08 Javascript
vue登录以及权限验证相关的实现
2019/10/25 Javascript
Ant design vue table 单击行选中 勾选checkbox教程
2020/10/24 Javascript
Vue如何循环提取对象数组中的值
2020/11/18 Vue.js
Python使用MONGODB入门实例
2015/05/11 Python
Python中使用Queue和Condition进行线程同步的方法
2016/01/19 Python
利用python 更新ssh 远程代码 操作远程服务器的实现代码
2018/02/08 Python
基于Python在MacOS上安装robotframework-ride
2018/12/28 Python
Python优秀开源项目Rich源码解析的流程分析
2020/07/06 Python
python Autopep8实现按PEP8风格自动排版Python代码
2021/03/02 Python
利用CSS3的flexbox实现水平垂直居中与三列等高布局
2016/09/12 HTML / CSS
英国鲜花速递:Serenata Flowers
2018/04/03 全球购物
eVitamins日本:在线购买折扣维生素、补品和草药
2019/04/04 全球购物
统计每一学生的平均成绩
2014/06/06 面试题
常用UNIX 命令(Linux的常用命令)
2015/12/26 面试题
工作表现自我评价
2014/02/08 职场文书
小学师德标兵先进事迹材料
2014/05/25 职场文书
小学三八妇女节活动总结
2015/02/06 职场文书
上级领导检查欢迎词
2015/09/30 职场文书
导游词之清晏园
2019/11/22 职场文书
python批量更改目录名/文件名的方法
2021/04/18 Python
go原生库的中bytes.Buffer用法
2021/04/25 Golang
oracle重置序列从0开始递增1
2022/02/28 Oracle
Python中npy和mat文件的保存与读取
2022/04/24 Python