flask 应用的基本结构:

htmlweb.py
-- static
-- templates

bootstrap.min.css 放到 static 文件夹下,在 templates 文件夹下新建 index.html,里面写入如下信息:

<html>
<head>
<title>APIParse</title>
<link rel="stylesheet" type="text/css" href="{{url_for('static', filename='css/bootstrap.min.css')}}"/>
</head>
<body>
TTYB
</body>
</html>

htmlweb.py 中写入如下内容:

from flask import Flask, render_template
from io import BytesIO
import xlsxwriter
def create_workbook():
output = BytesIO()
# 创建Excel文件,不保存,直接输出
workbook = xlsxwriter.Workbook(output, {'in_memory': True})
# 设置Sheet的名字为download
worksheet = workbook.add_worksheet('download')
# 列首
title = ["col1","col2","col3"]
worksheet.write_row('A1', title)
dictList = [{"a":"a1","b":"b1","c":"c1"},{"a":"a2","b":"b2","c":"c2"},{"a":"a3","b":"b3","c":"c3"}]
for i in range(len(dictList)):
row = [dictList[i]["a"],dictList[i]["b"],dictList[i]["c"]]
worksheet.write_row('A' + str(i + 2), row)
workbook.close()
response = make_response(output.getvalue())
output.close()
return response app = Flask(__name__) @app.route('/', methods=['GET'])
def index():
return render_template("index.html") from flask import make_response
@app.route('/download', methods=['GET'])
def download(): response = create_workbook()
response.headers['Content-Type'] = "utf-8"
response.headers["Cache-Control"] = "no-cache"
response.headers["Content-Disposition"] = "attachment; filename=download.xlsx"
return response if __name__ == "__main__":
app.run(host='127.0.0.1', port=88, debug=True)

运行在浏览器访问 127.0.0.1:88 可以看到新建的页面,在页面访问 127.0.0.1/download 可以下载生成的 excel :

flask下载excel的更多相关文章

  1. django下载excel,使用django-excel插件

    django下载Excel,使用django-excel插件 由于目前的资料多是使用pandas或xlwt库实现的.其实没有那么的麻烦,因为django有相对应的插件django-excel. 该插件 ...

  2. 360浏览器下载excel问题解决方式

    亲们有没有碰到过今天我遇到的这件事. 如果使用简单的链接.或者get方式提交的表单,去下载excel,那么360浏览器就会有问题. 问题是:它没把我用java生成的excel表格下载,而是去把我的列表 ...

  3. java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)

    使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...

  4. 在线读取Mongodb数据库下载EXCEL文件

    版本:Mongodb2.4.8 通过页面下载Excel文件 jsp <%@ page language="java" contentType="text/html; ...

  5. angularJS通过post方法下载excel文件

    最近工作中遇到,要使用angularJS的post方法来下载excel的情况.网上找到一个帖子:http://stackoverflow.com/questions/22447952/angularj ...

  6. asp.net 下载Excel (数据流,不保存)--客户端

    效果图: 前端页面 <html> <head> <title>Test For Excel</title> <script src="j ...

  7. Angularjs 通过WebApi 下载excel

    如果想知道 AngularJs 通过WebAPI 下载Excel.请看下文,这里仅提供了一种方案. 服务器端代码如下: protected HttpResponseMessage GenereateE ...

  8. 使用DateSet下载Excel

    这里我们使用Microsoft.Office.Interop.Excel.dll下载Excel,没有引用可点击下载 关键代码,ExcelHelper类 using System; using Syst ...

  9. 前端axios下载excel(二进制)

    需求:通过后端接口下载excel文件,后端没有文件地址,返回二进制流文件 实现:axios(ajax类似) 主要代码: axios:设置返回数据格式为blob或者arraybuffer 如: var ...

随机推荐

  1. Mysql分页查询性能分析

    [PS:原文手打,转载说明出处,博客园] 前言 看过一堆的百度,最终还是自己做了一次实验,本文基于Mysql5.7.17版本,Mysql引擎为InnoDB,编码为utf8,排序规则为utf8_gene ...

  2. git push The requested URL returned error: 403 Forbidden while accessing

    错误提示信息: error: The requested URL returned error: Forbidden while accessing https://github.com/xingfu ...

  3. Invoke-ASCmd 部署SSAS database

    Install-Module -Name SqlServer -RequiredVersion 21.0.17099 -AllowClobberInvoke-ASCmd -Server 10.162. ...

  4. Windows下配置vue的环境

    最近在学习vue.js,希望前端能用vue来作为主要框架.这里记录一下NPM在Windows中安装过程. 下载安装 下载地址 下载v6.11.0 LTS稳定版. 在C盘创建nodejs目录,并进行安装 ...

  5. [Micropython]TPYBoard v10x MFRC522智能门禁系统

    MF RC522 是应用于13.56MHz 非接触式通信中高集成度读写卡系列芯片中的一员.也就是射频卡. 经常忘带钥匙,最尴尬的上周竟然去开4楼的门,(家住五楼,无电梯),开了好一会没打开,事后对4楼 ...

  6. HTML 标签小细节

    简书地址:https://www.jianshu.com/p/03a23aa28a34 今天重新学习了一下HTML中标签的用法,补充并记录一下自己新学到的知识. a中的href href Contai ...

  7. Java多线程:线程间通信之Lock

    Java 5 之后,Java在内置关键字sychronized的基础上又增加了一个新的处理锁的方式,Lock类. 由于在Java线程间通信:volatile与sychronized中,我们已经详细的了 ...

  8. 编程之美2.18 数组分割 原创解O(nlogn)的时间复杂度求解:

    题目:有一个无序.元素个数为2n的正整数组,要求:如何能把这个数组分割为元素个数为n的两个数组,并使两个子数组的和最接近? 1 1 2 -> 1 1 vs  2 看题时,解法的时间复杂度一般都大 ...

  9. msql索引

    从网上找了两种解决方案: 最近要给一个表加一个联合唯一索引,但是表中的两个联合健有重复值,导致无法创建: 解决方案一:ignore(会删除重复的记录(重复记录只保留一条,其他的删除),然后建立唯一索引 ...

  10. Maven学习(六)-- Maven与Eclipse整合

    由于我使用的是IDEA所以就不摘录了,感兴趣的移步 Maven学习总结(六)--Maven与Eclipse整合 Maven学习总结(七)--eclipse中使用Maven创建Web项目