python xlwt xlsxwriter

  1 # xlsxwtiter
  2 try:
  3     output = BytesIO()
  4     wb = xlsxwriter.Workbook(output)
  5     sheet = wb.add_worksheet('audit-sheet')
  6     head_cell_xf = wb.add_format({
  7         'font_name': 'SimSun',
  8         'bold': True,
  9         'text_wrap': True,
 10         'valign': 'vcenter',
 11         'align': 'left',
 12         'bg_color': 'red',
 13         'pattern': 1,
 14         'bottom': 1,
 15         'left': 1,
 16         'right': 1,
 17         'top': 1,
 18     })
 19 
 20     body_cell_xf = wb.add_format({
 21         'font_name': 'SimSun',
 22         'valign': 'vcenter',
 23         'align': 'left',
 24 
 25         'bg_color': 'gray',
 26         'pattern': 1,
 27         'bottom': 1,
 28         'left': 1,
 29         'right': 1,
 30         'top': 1,
 31     })
 32 
 33     head_data = ['帐号名', '操作模块', '操作时间', '操作类型', '请求url', '请求方法', '请求结果', '请求描述', '请求内容']
 34     sheet.write_row('A1', head_data, head_cell_xf)
 35     data_row = 2
 36     rsp_data = json.loads(rsp.content)['result']["audits"]
 37     for item in rsp_data:
 38         temp = []
 39         action_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(item.get("action_timestamp", "")))
 40         temp.append(item.get("account_name", ""))
 41         temp.append(item.get("action_module", ""))
 42         temp.append(action_time)
 43 
 44         temp.append(item.get("action_type", ""))
 45         temp.append(item.get("action_url", ""))
 46         temp.append(item.get("action_method", ""))
 47         temp.append(item.get("action_result", ""))
 48         temp.append(item.get("action_summary", ""))
 49         temp.append(item.get("action_content", ""))
 50         sheet.write_row('A' + str(data_row), temp, body_cell_xf)
 51         data_row += 1
 52 
 53     wb.close()
 54 
 55     response = HttpResponse(output.getvalue())
 56     response['content_type'] = 'application/vnd.ms-excel'
 57     response['Content-Disposition'] = 'attachment;filename=audit.xls'
 58     response.write(output.getvalue())
 59     return response
 60 
 61 except Exception as e:
 62     LOG.error("导出execl失败 %s" % e)
 63     return Response({"status": rsp.status, "message": "导出execl失败", "result": {}, "module": "audit"})
 64 
 65 
 66 # xlwt
 67 try:
 68     style_heading = xlwt.easyxf("""
 69                         font:
 70                             name Arial,
 71                             colour_index white,
 72                             bold on,
 73                             height 0xA0;
 74                         align:
 75                             wrap off,
 76                             vert center,
 77                             horiz center;
 78                         pattern:
 79                             pattern solid,
 80                             fore-colour 0x19;
 81                         borders:
 82                             left THIN,
 83                             right THIN,
 84                             top THIN,
 85                             bottom THIN;
 86                         """)
 87 
 88     wb = xlwt.Workbook(encoding='utf8')
 89     sheet = wb.add_sheet('audit-sheet')
 90     sheet.write(0, 0, '帐号名', style_heading)
 91     sheet.write(0, 1, '操作模块', style_heading)
 92     sheet.write(0, 2, '操作时间', style_heading)
 93     sheet.write(0, 3, '操作类型', style_heading)
 94     sheet.write(0, 4, '请求url', style_heading)
 95     sheet.write(0, 5, '请求方法', style_heading)
 96     sheet.write(0, 6, '请求结果', style_heading)
 97     sheet.write(0, 7, '请求描述', style_heading)
 98     sheet.write(0, 8, '请求内容', style_heading)
 99 
100     data_row = 1
101     rsp_data = json.loads(rsp.content)['result']["audits"]
102     for item in rsp_data:
103         action_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(item.get("action_timestamp", "")))
104         sheet.write(data_row, 0, item.get("account_name", ""))
105         sheet.write(data_row, 1, item.get("action_module", ""))
106         sheet.write(data_row, 2, action_time)
107         sheet.write(data_row, 3, item.get("action_type", ""))
108         sheet.write(data_row, 4, item.get("action_url", ""))
109         sheet.write(data_row, 5, item.get("action_method", ""))
110         sheet.write(data_row, 6, item.get("action_result", ""))
111         sheet.write(data_row, 7, item.get("action_summary", ""))
112         sheet.write(data_row, 8, item.get("action_content",""))
113         data_row += 1
114 
115     output = BytesIO()
116     wb.save(output)
117     output.seek(0)
118     response = HttpResponse(output.getvalue())
119     response['content_type'] = 'application/vnd.ms-excel'
120     response['Content-Disposition'] = 'attachment;filename=audit.xlsx'
121     response.write(output.getvalue())
122     return response
123 except Exception as e:
124     LOG.error("导出execl失败 %s" % e)
125     return Response({"status": rsp.status, "message": "导出execl失败", "result": {}, "module": "audit"})

 

posted @ 2019-09-26 11:31  傲娇的小怪兽啊  阅读(390)  评论(0编辑  收藏  举报