flask 导出excel
Flask 导出Excel 的两种方法 ,第一种使用Flask-Excel
安装:
pip install Flask-Excel
pip install pyexcel-xls
pip install pyexcel-xlsx
pip install pyexcel-ods
除了安装主要的Falsk-Excel,还需要安装一些格式的扩展,需要导出什么格式就安装什么扩展
使用:
#extendsions.py
import flask_excel as excel#__init__.py
from hifeiji.extendsions import excel
excel.init_excel(app)#blueprint
#activity.py
import flask_excel as excel@activity_bp.route("/export", methods=['GET'])
@login_required
def export_records():content = [['No','Title','Name','Tel','Start','End','Service Time']]activityList = ActivityAtten.query.order_by(ActivityAtten.activity_start.asc()).all()if activityList:for value in activityList:new_content = []new_content = [value.activity_no,value.activity_title,value.activity_atten_name,value.activity_atten_tel,value.activity_start,value.activity_end,minutes2hours(value.activity_servicetime)]content.append(new_content)current_app.logger.info("exportActivity")return excel.make_response_from_array(content, "xlsx",file_name="exportActivity")
首先,要初始化Flask-Excel,
excel.init_excel(app)
重点是最后一句
return excel.make_response_from_array(content, "xlsx",file_name="exportActivity") #注释是这样的
(function) make_response_from_array: (array, file_type, status=200, file_name=None, **keywords) -> None
第二种是使用 xlsxwriter
首先安装
pip install xlswriter
xlsxwriter 不需要初始化,可以直接调用,代码如下
import xlsxwriter
import io@activity_bp.route("/exportActivity",methods=['GET'])
@login_required
def exportActivity():output = io.BytesIO()workbook = xlsxwriter.Workbook(output)sheet = workbook.add_worksheet('考勤人员')activityList = Activity.query.order_by(Activity.starttime.asc()).all()format1 = {# 'bold': True, # 字体加粗'align': 'center', # 水平位置设置:居中'valign': 'vcenter', # 垂直位置设置,居中# 'font_size': 14, # '字体大小设置''border':1}title_format = workbook.add_format(format1)#构建格式并添加入实例format2={# 'bold':True,#字体加粗# 'num_format':'$#,##0',#货币数字显示样式'align':'center',#水平位置设置:居中'valign':'vcenter',#垂直位置设置,居中# 'font_size':16,#'字体大小设置'# 'font_name':'Courier New',#字体设置# 'italic':True,# 斜体设置# 'underline':1,#下划线设置 1.单下划线 2.双下划线 33.单一会计下划线 34双重会计下划线# 'font_color':"red",#字体颜色设置'border':1,#边框设置样式1# 'border_color':'green',#边框颜色# 'bg_color':'#c7ffec',#背景颜色设置}content_format = workbook.add_format(format2)row = 0number = 1if activityList:for index in range(len(activityList)):activityAttenList = ActivityAtten.query.filter_by(activity_no=activityList[index].no).all()if activityAttenList:title_date = time.strftime("%Y-%m-%d", time.strptime(str(activityList[index].starttime), "%Y-%m-%d %H:%M:%S")) title = activityList[index].title# sheet.merge_range('B'+str(row)+':F'+str(row),str(index+1)+'.'+str(title_date)+' '+title)sheet.merge_range(row,1,row,5,str(number)+'.'+str(title_date)+' '+title,title_format)row = row+1sheet.set_column(row,1, 5) sheet.set_column(row,2, 10)sheet.set_column(row,3, 20)sheet.set_column(row,4, 20)sheet.set_column(row,5, 10)sheet.write(row,1,'序号',title_format)sheet.write(row,2,'姓名',title_format)sheet.write(row,3,'手机号码',title_format)sheet.write(row,4,'服务/培训时长',title_format)sheet.write(row,5,'备注',title_format)row = row+1for key in range(len(activityAttenList)):sheet.write(row,1,str(key+1),title_format)sheet.write(row,2,activityAttenList[key].activity_atten_name,title_format)sheet.write(row,3,activityAttenList[key].activity_atten_tel,title_format)sheet.write(row,4,minutes2hours(activityAttenList[key].activity_servicetime),title_format)sheet.write(row,5,'',title_format)row = row+1row = row+2 #每个活动换2行number = number+1 #活动序号workbook.close()response = make_response(output.getvalue())response.headers['Content-Type'] = "application/x-xlsx"response.headers["Cache-Control"] = "no-cache"response.headers["Content-Disposition"] = "attachment; filename=download.xlsx"return response
有几个点需要注意,如果想设置单元格的样式,可以这样:
format1 = {# 'bold': True, # 字体加粗'align': 'center', # 水平位置设置:居中'valign': 'vcenter', # 垂直位置设置,居中# 'font_size': 14, # '字体大小设置''border':1}title_format = workbook.add_format(format1)#.......sheet.write(row,1,'序号',title_format)
write() 的方法是这样的
#跟踪 write()方法,是这样的def write(self, row, col, *args):"""Write data to a worksheet cell by calling the appropriate write_*()method based on the type of data being passed.Args:row: The cell row (zero indexed).col: The cell column (zero indexed).*args: Args to pass to sub functions.Returns:0: Success.-1: Row or column is out of worksheet bounds.other: Return value of called method."""return self._write(row, col, *args)
行和列都从0 开始,比如A1,其实就是0,0
make_response() 方法使用时,要注意,是使用flask 中的make_response()方法,而不是pyexcel 中的 make_response(),当初我就是在这里卡住了!两个方法是不一样的。
跟住make_response()方法
def make_response(*args: t.Any) -> "Response":"""Sometimes it is necessary to set additional headers in a view. Becauseviews do not have to return response objects but can return a value thatis converted into a response object by Flask itself, it becomes tricky toadd headers to it. This function can be called instead of using a returnand you will get a response object which you can use to attach headers.If view looked like this and you want to add a new header::def index():return render_template('index.html', foo=42)You can now do something like this::def index():response = make_response(render_template('index.html', foo=42))response.headers['X-Parachutes'] = 'parachutes are cool'return responseThis function accepts the very same arguments you can return from aview function. This for example creates a response with a 404 errorcode::response = make_response(render_template('not_found.html'), 404)The other use case of this function is to force the return value of aview function into a response which is helpful with viewdecorators::response = make_response(view_function())response.headers['X-Parachutes'] = 'parachutes are cool'Internally this function does the following things:- if no arguments are passed, it creates a new response argument- if one argument is passed, :meth:`flask.Flask.make_response`is invoked with it.- if more than one argument is passed, the arguments are passedto the :meth:`flask.Flask.make_response` function as tuple... versionadded:: 0.6"""if not args:return current_app.response_class()if len(args) == 1:args = args[0]return current_app.make_response(args)
从注释可以知道,response = make_response(render_template('index.html', foo=42))可以使用html 转存生成其他文件
以上是两种导出excel 的方法,
如果只是单纯导出报表,无需其他样式的,可以使用flask-excel,因为比较简单方便。
如果是需要有一定样式的,比如合拼单元格之类的,xlsxwriter 好用
flask 导出excel相关推荐
- flask导出Excel报表详解
在日常开发中,导出数据报表可谓必备技能,在后台管理中,很多模块都需要数据报表,现在我们一起来学习一下 flask 如何导出数据报表. 没有实例的讲解很不容易理解,本文我们依然从实际项目来讲解,对 &q ...
- flask使用tablib导出excel数据表
在网页中常常有导出数据的需求,尤其是一下管理类平台.在flask中要导出excel数据表,通常可以使用xlwt库,创建文件并逐行写入数据,但是使用起来总是感觉很麻烦.tablib库相对操作更加方便. ...
- mvc npoi导出excel ajax,ASP.Net MVC利用NPOI导入导出Excel
因近期项目遇到所以记录一下: 首先导出Excel: 首先引用NPOI包 http://pan.baidu.com/s/1i3Fosux (Action一定要用FileResult) /// /// 批 ...
- java struts2 excel上传_Java Struts2 实现数据库数据导出Excel文件
HTML: 导出 Struts.xml true application/vnd.ms-excel;charset=GBK excelStream attachment;filename=${file ...
- Java springMVC POI 导出 EXCEL
2019独角兽企业重金招聘Python工程师标准>>> 思路 : 将需要导出的数据存放在一个List中 创建一个EXCEL表 注意 XSSFWorkbook 只能操作2007以上的版 ...
- 如何优雅的导出 Excel
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:juejin.im/post/5c6b6b126fb9a04 ...
- Java报表工具FineReport导出EXCEL的四种API
在实际的应用中会经常需要将数据导出成excel,导出的方式除原样导出还有分页导出.分页分sheet导出和大数据量导出.对于excel 2003版,由于限制了每个sheet的最大行数和列数,大数据量导出 ...
- html导出excel时换行符,ASP.NET 导出到Excel时保留换行的代码
完整代码: protected void Button1_Click(object sender, EventArgs e) { System.Web.HttpContext curContext = ...
- java 导出excel 注解_Java基于注解和反射导入导出Excel
list = ei.getDataList(User.class); for (User user : list){ try{ //to do: 保存/处理数据 //userService.save( ...
- 导出excel 数字前少0_【产品介绍】数字压力校验仪
数字压力校验仪 24V供电 + (4-20)Ma测量 调节7位设置数字显示 | 可拆卸充电锂电池 现场.实验室压力校准系统CWY300数字压力校验仪,是我公司推出的新一代高精度.高稳定.兼具工业4.0 ...
最新文章
- python until怎么用不了_为何你还不懂得如何使用Python协程
- python3 PIL、opencv, 二进制、base64 四种图片格式转换
- Java 读写文件大全
- 杂项-Log:NLog
- Ant基础介绍(转载)
- mysql备份与还原-mysqldump备份、mysql与source还原
- Valgrind 安装与使用
- linux 启动程序 绑定id,linux如何根据进程ID查找启动程序的路径
- 怎么创建数据表的实体类和业务类_SSM搭建二手市场交易平台(二):数据表设计...
- java u0002_老玩法,输出金字塔
- 第一台全自动电子计算机,关于世界上第一台电子计算机ENIAC的叙述错误的是() senny全自动微电脑水位控制仪...
- 使用redis保存验证码
- 通达信版弘历软件指标_弘历主图指标详解 通达信指标
- st8s003 c语言编译器,ST系列STM8S003F3P6单片机芯片介绍
- android 校验手机号码,检查Android中的有效手机号码
- Tomcat中的ResourceBundle国际化解析
- android蓝牙取sbc音频数据
- mysql sock_mysql.sock 文件解析
- 求偶数c语言程序,用C语言编写一道程序计算100以内偶数的和
- 镇魔曲网页版服务器选择,镇魔曲网页版职业选择解析 哪个职业好