总体思路:基于bootstrap4的前端页面上传组件,把excel文件上传至服务器,并利用python pandas读取里面的数据形成字典列表

通过pymongo 接口把数据插入或追加到mongodb相关集合中

  • html部分
<input type="file"  id="excelfile" class="form-control">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="rebuild" value="option1">
<label class="form-check-label" for="rebuild">重建</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="inlineRadioOptions" id="append" value="option2">
<label class="form-check-label" for="append">附加</label>
</div>
........
<button type="button" id="postbtn" class="btn btn-lg btn-block btn-outline-primary">Sign up for free</button>
  • jquery部分:其中24-26行是确保ajax提交成功的关键
 //radio按键,按下和抬起所代表的不同动作
$(".form-check-input").click(function () {
if(typeof($(this).attr("checked"))=="undefined")
{
$(this).attr("checked","checked")
}
else
{
$(this).removeAttr('checked')
}
}); $("#postbtn").click(function () {
//根据选中的radio,执行相应的操作,追加mongodb的数据集或是
//删除重建
var param = $(".form-check-input:checked").attr("id");
//alert("参数"+param);
var dataform = new FormData();
dataform.append('fileobj',$("#excelfile")[].files[]);
dataform.append('status',param);
$.ajax({
url:'/qingxiniaodao',
type:'post',
mimeType:'multipart/form-data',
contentType:false,
processData:false,
data:dataform,
success:function (data) {
console.log(data)
}
})
});
</script>
  • tornado----python部分
class qingxiniaodao(RequestHandler):
def get(self):
self.render('mongowithpanda.html')
def post(self):
if self.request.files:
#第一步上传文件
filename = self.request.files['fileobj'][]['filename']
param =self.get_argument("status")#如果值为rebuild就重建,append为附加
filename =currentpath+"\\temfile\\"+filename
with open(filename,"wb") as writer:
writer.write(self.request.files['fileobj'][]['body'])
#第二步:读取文件生成dataframe
mydataframe = pandas.read_excel(filename)
print(type(mydataframe.to_json(orient='records')))
self.write(json.dumps({"rets": mydataframe.to_json(orient='records')}))
#第三步获取集合名称----默认每个excel文件只有一个sheet
collectioname = pandas.ExcelFile(filename)
#获取sheet名
print(collectioname.sheet_names[])
#mongodb操作:
client = MongoClient("localhost",)
db = client['olddream']
cols = db[collectioname.sheet_names[]]
if(param=="rebuild"):
cols.remove()
title = []
rets = []
superdict = {}
retlist = []
print("这是表头:", mydataframe.columns)
for i in mydataframe.columns:
title.append(i)
# mydataframe.values----结果集ndarray类型
print("算出数据集的行,列数:", mydataframe.values.shape)
rowcount = mydataframe.values.shape[] # ndarray的行数,--元组
i =
print("打印数据集的值:", mydataframe.values)
print("打印数据集的某行某列:", mydataframe.values[][])
k =
for i in range(, rowcount):
for k in range(, len(title)):
superdict[title[k]] = mydataframe.values[i][k]
retlist.append(superdict)
superdict = {}
i +=
self.write(json.dumps({"rets": retlist}))
insertrows=cols.insert_many(retlist)
self.write(json.dumps({"rets":str(insertrows.inserted_ids)}))
05-07 15:16
查看更多