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 %}
<script type="text/javascript" src="/static/scripts/jquery/jquery-1.7.1.js"></script>
<link href="/static/style/authority/basic_layout.css" rel="stylesheet" type="text/css">
<link href="/static/style/authority/common_style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/static/scripts/authority/commonAll.js"></script>
<script type="text/javascript" src="/static/scripts/fancybox/jquery.fancybox-1.3.4.js"></script>
<script type="text/javascript" src="/static/scripts/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<link rel="stylesheet" type="text/css" href="/static/style/authority/jquery.fancybox-1.3.4.css" media="screen"></link>
<script type="text/javascript" src="/static/scripts/artDialog/artDialog.js?skin=default"></script>

<div id="container">
<div class="ui_content">
<div class="ui_text_indent">
<div id="box_border">
<div id="box_top">搜索</div>
  机房名字  <input type="text" id="mname" name="mname" value="{{ mname }}" class="ui_input_txt02" />
</div>
<div id="box_top">新增/编辑</div>
<form id="submitForm" name="submitForm" method="post" action="add_machinaroom.html">
<input type="hidden" id="machinaroomid" name="machinaroomid"/>
  机房名字  <input type="text" id="machinaroomname" name="machinaroomname" class="ui_input_txt02" />
  机房位置  <input type="text" id="machinaroomlocation" name="machinaroomlocation" class="ui_input_txt02" />
  机房VPN  <input type="text" id="machinaroomvpnurl" name="machinaroomvpnurl" class="ui_input_txt02" />
  备注  <input type="text" id="machinaroommemo" name="machinaroommemo" class="ui_input_txt02" />
</div>
<div id="box_bottom">
<div class="pagination">
<span class="current">
{% if machinaroomlist.has_previous %}
<a href="javascript:void(0)" onclick="search_machina({{ machinaroomlist.previous_page_number }});">上一页</a>
{% endif %}

    <span class="current">
        Page {{ machinaroomlist.number }} of {{ machinaroomlist.paginator.num_pages }}.
    </span>

    {% if machinaroomlist.has_next %}
        <a href="javascript:void(0)" onclick="search_machina({{ machinaroomlist.next_page_number }});">下一页</a>
    {% endif %}
</span>

</div>
<input type="button" value="查询" class="ui_input_btn01" onclick="search_machina(1);"/>
<input type="submit" value="新增" class="ui_input_btn01" />
<input type="button" value="编辑" class="ui_input_btn01" onclick="edit_machinaroom({{ machinaroomlist.number }});" />
<input type="button" value="删除" class="ui_input_btn01" onclick="batdel_machinaroom();" />
<input type="button" value="导出EXCEL" class="ui_input_btn01" onclick="excel_machinaroom();" />
</div>
</form>
</div>
</div>
{% endblock %}

{% block content %}

<div class="ui_content">
<div class="ui_tb">
<table class="table" cellspacing="0" cellpadding="0" width="100%" align="center" border="0">
<tr>
<th width="30"><input type="checkbox" id="id" name="id" />
</th>
<th>机房名字</th>
<th>机房位置</th>
<th>VPN链接</th>
<th>备注</th>
<th>操作</th>
</tr>
{% for mr in machinaroomlist.object_list %}
<tr>
<td><input type="checkbox" name="idcheck" value={{ mr.id }} class="acb" /></td>
<td>{{ mr.name }}</td>
<td>{{ mr.location }}</td>
<td>{{ mr.vpnurl }}</td>
<td>{{ mr.memo }}</td>
<td>
<a href="javascript:void(0)" onclick="edit_mr('{{ mr.id }}','{{ mr.name }}','{{ mr.location }}','{{ mr.vpnurl }}','{{ mr.memo }}')" class="edit">编辑</a>
<a href="javascript:void(0)" onclick="del_machinaroom({{ mr.id }},{{ machinaroomlist.number }});">删除</a>
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
<script type="text/javascript">
function search_machina(page){
var text1 = document.getElementById("mname").value;
var myurl="search_machinaroom.html"+"?"+"mname="+text1+"&&page="+page;
window.location.assign(encodeURI(myurl))
}
function edit_mr(id,name,ll,vpnurl,memo){
document.getElementById("machinaroomid").value=id;
document.getElementById("machinaroomname").value=name;
document.getElementById("machinaroomlocation").value=ll;
document.getElementById("machinaroomvpnurl").value=vpnurl;
document.getElementById("machinaroommemo").value=memo;
}
function edit_machinaroom(page){
var text1 = document.getElementById("mname").value;
id=document.getElementById("machinaroomid").value;
name=document.getElementById("machinaroomname").value;
ll=document.getElementById("machinaroomlocation").value;
vpnurl=document.getElementById("machinaroomvpnurl").value;
memo=document.getElementById("machinaroommemo").value;
var myurl="edit_machinaroom.html"+"?"+"id="+id+"&&name="+name+"&&location="+ll+"&&vpnurl="+vpnurl+"&&memo="+memo+"&&mname="+text1+"&&page="+page;
window.location.assign(encodeURI(myurl))
}
function del_machinaroom(id,page){
if(window.confirm('确定要删除该记录吗?')){
var text1= document.getElementById("mname").value;
var myurl="del_machinaroom.html"+"?"+"id="+id+"&&mname="+text1+"&&page="+page;
window.location.assign(encodeURI(myurl))
return true;
}else{
//alert("取消");
return false;
}
}
function batdel_machinaroom(){
if(window.confirm('确定要删除记录吗?')){
obj = document.getElementsByName("idcheck");
var text1= document.getElementById("mname").value;
check_val = [];
for(k in obj){
if(obj[k].checked)
check_val.push(obj[k].value);
}
var myurl="batdel_machinaroom.html"+"?"+"ids="+check_val+"&&mname="+text1;
window.location.assign(encodeURI(myurl))
return true;
}else{
return false;
}
}
function excel_machinaroom(){
var text1 = document.getElementById("mname").value;
var myurl="excel_machinaroom.html"+"?"+"mname="+text1;
window.location.assign(encodeURI(myurl))
}
</script>
{% endblock %}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,064评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,606评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,011评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,550评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,465评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,919评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,428评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,075评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,208评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,185评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,191评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,914评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,482评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,585评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,825评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,194评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,703评论 2 339

推荐阅读更多精彩内容