python前端生成excel

来源:互联网 发布:淘宝客微商城 编辑:程序博客网 时间:2024/05/17 22:20
#前端html:<div> <a  href="{{excelpara}}" class="btn export">导出1</a></div>*************************************************************************************************前端js:
$scope.export = function(){     $scope.excelpara="/rpc/genexcel?date_from=" + $scope.date_from + "&date_to="+$scope.date_to;     }   
*************************************************************************************************#后台:from flask import Flask, request, jsonifyfrom flask_jsonrpc import JSONRPCimport simplejson as jsonfrom frontpag_pg import dataToPg,dataUpdatefrom export_1 import genXlsapp = Flask(__name__)@app.route("/rpc/genexcel",methods=["GET","POST"])def gexecel(): date_from=request.values.get('date_from') date_to=request.values.get('date_to') responce = genXls(date_from,date_to) return responceif __name__=='__main__': app.debug =True # app.run(host = "127.0.0.1", port = 8888)*************************************************************************************************#genXls方法:def genXls(date_from,date_to): d = tablib.Databook() ISOTIMEFORMAT='%Y%m%d%H%M%S' # timestr = time.strftime( ISOTIMEFORMAT, time.localtime() ) filename = "test.xls" data = export(date_from,date_to) data = tablib.Dataset(*data, headers=headers) d.add_sheet(data) output=StringIO.StringIO() output.write(d.xls) response = make_response(output.getvalue()) response.headers['Content-Type'] = 'application/vnd.ms-excel' response.headers['Content-Disposition'] = 'attachment; filename=' + filename return response

0 0
原创粉丝点击