Django 搭建CMDB系統(tǒng)完整[9](導(dǎo)出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:
# 創(chuàng)建工作薄
ws = Workbook(encoding='utf-8')
w = ws.add_sheet(u"機(jī)房清單")
w.write(0, 0, "id")
w.write(0, 1, u"機(jī)房名字")
w.write(0, 2, u"機(jī)房位置")
w.write(0, 3, u"VPN")
w.write(0, 4, u"備注")
# 寫入數(shù)據(jù)
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=機(jī)房清單-'+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>
??機(jī)房名字??<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"/>
??機(jī)房名字??<input type="text" id="machinaroomname" name="machinaroomname" class="ui_input_txt02" />
??機(jī)房位置??<input type="text" id="machinaroomlocation" name="machinaroomlocation" class="ui_input_txt02" />
??機(jī)房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 }});">上一頁(yè)</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 }});">下一頁(yè)</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="導(dǎo)出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>機(jī)房名字</th>
<th>機(jī)房位置</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('確定要?jiǎng)h除該記錄嗎聘芜?')){
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('確定要?jiǎng)h除記錄嗎兄渺?')){
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 %}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市汰现,隨后出現(xiàn)的幾起案子瞎饲,更是在濱河造成了極大的恐慌妄田,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,406評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異珊泳,居然都是意外死亡旨椒,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)米罚,“玉大人钧汹,你說(shuō)我怎么就攤上這事隘竭。” “怎么了?”我有些...
    開封第一講書人閱讀 167,815評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵仇轻,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng)臣淤,這世上最難降的妖魔是什么姓蜂? 我笑而不...
    開封第一講書人閱讀 59,537評(píng)論 1 296
  • 正文 為了忘掉前任卿堂,我火速辦了婚禮穗慕,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘暑脆。我一直安慰自己碟联,他們只是感情好妓美,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,536評(píng)論 6 397
  • 文/花漫 我一把揭開白布凯正。 她就那樣靜靜地躺著,像睡著了一般允睹。 火紅的嫁衣襯著肌膚如雪胁澳。 梳的紋絲不亂的頭發(fā)上该互,一...
    開封第一講書人閱讀 52,184評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音听哭,去河邊找鬼慢洋。 笑死,一個(gè)胖子當(dāng)著我的面吹牛陆盘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播败明,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼隘马,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了妻顶?” 一聲冷哼從身側(cè)響起酸员,我...
    開封第一講書人閱讀 39,668評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎讳嘱,沒(méi)想到半個(gè)月后幔嗦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,212評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡沥潭,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,299評(píng)論 3 340
  • 正文 我和宋清朗相戀三年邀泉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片钝鸽。...
    茶點(diǎn)故事閱讀 40,438評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡汇恤,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出拔恰,到底是詐尸還是另有隱情因谎,我是刑警寧澤,帶...
    沈念sama閱讀 36,128評(píng)論 5 349
  • 正文 年R本政府宣布颜懊,位于F島的核電站财岔,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏河爹。R本人自食惡果不足惜匠璧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,807評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望昌抠。 院中可真熱鬧患朱,春花似錦、人聲如沸炊苫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至执虹,卻和暖如春拓挥,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背袋励。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工侥啤, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人茬故。 一個(gè)月前我還...
    沈念sama閱讀 48,827評(píng)論 3 376
  • 正文 我出身青樓盖灸,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親磺芭。 傳聞我的和親對(duì)象是個(gè)殘疾皇子赁炎,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,446評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容