flak导出excel表到本地

功能:点击按钮,下载一个excel表到本地。且该表有固定的模板样式

前端:

1.index.html

<!DOCTYPE html>


<html lang="en">
<head>
{% include 'header.html' %}
    <meta charset="UTF-8">
    <!-- 使用url_for加载静态文件 url_for第一个参数必须是'static',然后后面跟一个关键字参数filename='文件路径',从static文件夹下面开始寻找的 -->
    <link rel="stylesheet" href="{{ url_for('static',filename='css/index.css') }}">
    <!-- 使用url加载静态文件,以/开头的,浏览器会自动将前面的域名,端口号补齐 -->
    <!-- <link rel="stylesheet" href="/static/css/index.css"> -->
    <!-- 使用相对路径导入静态文件? 真的可以这样导入吗? -->

    <!-- <script src="{{ url_for('static',filename='js/index.js') }}"></script> -->
</head>
<body>
<p>static demo</p>
<img src="{{ url_for('static',filename='images/test.png') }}" alt="" width="500px" height="500px">
<!-- <img src="../static/images/test.png" alt="" width="500px" height="500px"> -->
        <form method="post" action="/export">
                <input type="submit" value="print" name="print"/>
        </form>
        <!--<div class="content">-->
            <!--<button  onclick="exportData()">导出报表</button>-->
        <!--</div>-->
</body>
 <!--导入footer.html -->
{% include 'footer.html' %}

    <script src="/static/js/jquery-1.11.1.min.js"></script>
    <script>
        $(function () {
            alert('hello world');
        });



        function exportData() {
            $.ajax({
                cache: true,
                type: "POST",
                url: "/export",
                async: false,
                success: function (result) {
                    console.log(result);
                    window.location.href = result;
                },
                complete: function () {
                },
                error: function (data) {
                    console.log('导出失败,请重试!');
                }
            });
        }
    </script>
</html>

后端:

1. exportdata.py

# !/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
点击按钮,下载一个excel表到本地。且写有固定内容
"""
from flask import Blueprint,send_file
import xlrd
from xlutils.copy import copy


bp = Blueprint("", __name__, url_prefix='/')

@bp.route('/export', methods=['POST'])
def exportExcel():
    rb = xlrd.open_workbook('static/excel/test.xls',formatting_info=True)
    wb = copy(rb)
    ws = wb.get_sheet(0)
    ws.write(2,0,'wangm')
    wb.save('static/excel/test1.xls')

    return send_file('static/excel/test1.xls')

2. manage.py

# -*- coding: UTF-8 -*-

import sys
from flask_script import Manager
from app import app
import flask_excel as excel

from werkzeug.utils import find_modules, import_string

reload(sys)
sys.setdefaultencoding('utf8')
manager = Manager(app)

def register_blueprints():
    modules = find_modules('api', recursive=True)
    for name in modules:
        module = import_string(name)
        if hasattr(module, 'bp'):
            app.register_blueprint(module.bp)

if __name__ == '__main__':
    register_blueprints()
    excel.init_excel(app)
    manager.run()
    #app.run()

3. app.py

# -*- coding: UTF-8 -*-

from flask import Flask,render_template

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')
原文地址:https://www.cnblogs.com/yoyoma0355/p/13913101.html