1,新增excel.py文件
路徑:xadmin/plugins/excel.py 內(nèi)容如下:
# -*- coding: utf-8 -*-
import xadmin
from xadmin.views import BaseAdminPlugin, ListAdminView
from django.template import loader
#excel 導(dǎo)入
class ListImportExcelPlugin(BaseAdminPlugin):
import_excel = False
# 入口函數(shù), 通過此屬性來指定此字段是否加載此字段
def init_request(self, *args, **kwargs):
return bool(self.import_excel)
# 如果加載, 則執(zhí)行此函數(shù)添加一個(gè) 導(dǎo)入 字段
def block_top_toolbar(self, context, nodes):
nodes.append(loader.render_to_string('xadmin/excel/model_list.top_toolbar.import.html'))
xadmin.site.register_plugin(ListImportExcelPlugin, ListAdminView)
2,新增model_list.op_toolbar.import.html文件
路徑:xadmin/templates/xadmin/excle/model_list.op_toolbar.import.html
內(nèi)容如下:
{% load i18n %}
<div class="btn-group export">
<a class="dropdown-toggle btn btn-default btn-sm" data-toggle="dropdown" href="#"><i class="icon-share"></i> 導(dǎo)入 <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
<li><a data-toggle="modal" data-target="#export-modal-import-excel"><i class="icon-circle-arrow-down"></i> 導(dǎo)入 Excel</a></li>
</ul>
<script>
function fileChange(target){
//檢測上傳文件的類型
var imgName = document.all.submit_upload.value;
var ext,idx;
if (imgName == ''){
document.all.submit_upload_b.disabled=true;
alert("請選擇需要上傳的 xls 文件!");
return;
} else {
idx = imgName.lastIndexOf(".");
if (idx != -1){
ext = imgName.substr(idx+1).toUpperCase();
ext = ext.toLowerCase( );
{# alert("ext="+ext);#}
if (ext != 'xls' && ext != 'xlsx'){
document.all.submit_upload_b.disabled=true;
alert("只能上傳 .xls 類型的文件!");
return;
}
} else {
document.all.submit_upload_b.disabled=true;
alert("只能上傳 .xls 類型的文件!");
return;
}
}
}
</script>
<div id="export-modal-import-excel" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">導(dǎo)入Excel</h4>
</div>
<div class="modal-body">
<input type="file" onchange="fileChange(this)" name="excel" id="submit_upload">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Close" %}</button>
<button class="btn btn-success" type="submit" id="submit_upload_b"><i class="icon-share"></i> 導(dǎo)入</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dalog -->
</div><!-- /.modal -->
</div>
3,重寫adminx.py的post函數(shù),用于接收上傳表格的請求,最后注冊xadmin中。其中上傳表格的處理邏輯在這里些,這里簡單的引入了APP的model。
from django.http import HttpResponseRedirect
from xlrd import open_workbook
import xadmin
from channel.models import Channel
class XadminChannel(object):
#import_excle = True在后臺頁面顯示上傳按鈕
import_excel = True
list_display = ['name',]
# 每頁顯示多少個(gè)
list_per_page = 20
# 配置在哪些字段搜索
search_fields = ['name']
# 配置過濾字段
list_filter = ['name']
# 導(dǎo)出字段
list_export_fields = ('name',)
def post(self, request, *args, **kwargs):
if 'excel' in request.FILES:
execl_file = request.FILES.get('excel')
wb = open_workbook(filename=None, file_contents=request.FILES['excel'].read())
table = wb.sheets()[0]
# sheet1 = wb.sheet_by_index(0)
rows = table.nrows #獲取行數(shù)
cols = table.ncols #獲取列數(shù)
for r in range(1,rows):
name = table.cell(r,0).value
bianmaqi = table.cell(r,1).value
jieru = table.cell(r, 2).value
type = table.cell(r, 3).value
pindao_id = table.cell(r, 4).value
beizhu = table.cell(r, 5).value
try:
a = Channel.objects.filter(name=name)
if a:
continue
elif name == None or name == '':
continue
else:
channel = Channel()
channel.name = name
channel.save()
except:
pass
return HttpResponseRedirect('http://127.0.0.1:8000/channel/channel/')
return super(XadminChannel, self).post(request, args, kwargs)
#APP注冊
xadmin.site.register(Channel, XadminChannel)