Django 搭建CMDB系统完整[9](导出EXCEL)

urls.py

url(r'^excel_machinaroom.html',machinaroomviews.excel_machinaroom,name='excel_machinaroom'),

views.py

def excel_machinaroom(request):
mname=request.GET.get('mname','0')
if mname=='0' or mname.strip()=='':
list_obj = Machinaroom.objects.all()
mname=''
else:
list_obj = Machinaroom.objects.filter(name=mname)
if list_obj:
# 创建工作薄
ws = Workbook(encoding='utf-8')
w = ws.add_sheet(u"机房清单")
w.write(0, 0, "id")
w.write(0, 1, u"机房名字")
w.write(0, 2, u"机房位置")
w.write(0, 3, u"VPN")
w.write(0, 4, u"备注")
# 写入数据
excel_row = 1
for obj in list_obj:
data_id = obj.id
data_user = obj.name
data_time = obj.location
data_content = obj.vpnurl
dada_source = obj.memo
w.write(excel_row, 0, data_id)
w.write(excel_row, 1, data_user)
w.write(excel_row, 2, data_time)
w.write(excel_row, 3, data_content)
w.write(excel_row, 4, dada_source)
excel_row += 1
sio = StringIO.StringIO()
ws.save(sio)
sio.seek(0)
response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=机房清单-'+mname+'.xls'
response.write(sio.getvalue())
return response

search_machinaroom.html

{% extends 'base.html' %}

{% block title %}











搜索

  机房名字  

新增/编辑



  机房名字  
  机房位置  
  机房VPN  
  备注  












{% endblock %}

{% block content %}












{% for mr in machinaroomlist.object_list %}








{% endfor %}

机房名字 机房位置 VPN链接 备注 操作
{{ mr.name }} {{ mr.location }} {{ mr.vpnurl }} {{ mr.memo }}
编辑
删除





{% endblock %}

你可能感兴趣的:(Django 搭建CMDB系统完整[9](导出EXCEL))