Django中如何用xlwt生成表格的方法步骤

作者:python学习网 时间:2023-07-17 07:47:12 

同样是做表格,但是有些人的表格就做的很好看。融合了之前所学不同模块的知识,来讲讲Django中生成表格的特殊方法。
这里只是mark一下导出的方法,并没有做什么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

来源:https://www.py.cn/kuangjia/django/20494.html

标签:Django,xlwt,生成表格
0
投稿

猜你喜欢

  • 解析:正确的理解SQL Server和XML支持

    2009-01-23 13:52:00
  • python使用epoll实现服务端的方法

    2021-05-16 22:52:34
  • Python使用time模块实现指定时间触发器示例

    2022-05-13 02:57:59
  • python数据处理之如何选取csv文件中某几行的数据

    2022-11-22 23:18:54
  • pytorch DataLoaderj基本使用方法详解

    2023-06-21 06:26:37
  • 如何把Mysql卸载干净(亲测有效)

    2024-01-16 09:06:06
  • python 使用openpyxl读取excel数据

    2021-02-10 07:32:10
  • Python中二维列表如何获取子区域元素的组成

    2021-02-16 02:41:01
  • 如何防止页面中的敏感信息被提取

    2008-05-04 11:59:00
  • Python实现学生管理系统的代码(JSON模块)

    2022-01-02 07:54:42
  • Python实现深度遍历和广度遍历的方法

    2023-10-13 20:41:13
  • Facebook是如何设计的[译]

    2009-09-17 13:10:00
  • 互联网科技大佬推荐的12本必读书籍

    2022-08-23 12:56:38
  • 浏览器是怎样工作的二:渲染引擎 HTML解析

    2012-05-09 20:34:20
  • 如何获知服务器上Application对象及其对应的值?

    2009-11-24 18:09:00
  • python递归函数绘制分形树的方法

    2021-04-22 02:16:02
  • 修复 Mac brew 安装 mongodb 报 Error: No available formula with the name ‘mongodb’ 问题详解

    2024-01-18 17:10:50
  • python 用opencv实现图像修复和图像金字塔

    2022-09-27 09:24:22
  • PHP中Http协议post请求参数

    2023-11-16 18:38:38
  • python 中open文件路径的选择问题解析

    2022-04-02 14:04:58
  • asp之家 网络编程 m.aspxhome.com