export excel

You can install it via pip:html

$ pip install Flask-Excel

or clone it and install it:python

$ git clone http://github.com/pyexcel/Flask-Excel.git
$ cd Flask-Excel
$ python setup.py install

Installation of individual plugins , please refer to individual plugin page. For example, if you need xls file support, please install pyexcel-xls:git

$ pip install pyexcel-xls

Setup

In your application, you must import it before using it:github

from flask.ext import excel

or:json

import flask.ext.excel

Quick start

A minimal application may look like this:flask

from flask import Flask, request, jsonify
from flask.ext import excel

app=Flask(__name__)

@app.route("/upload", methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        return jsonify({"result": request.get_array(field_name='file')})
    return '''
    <!doctype html>
    <title>Upload an excel file</title>
    <h1>Excel file upload (csv, tsv, csvz, tsvz only)</h1>
    <form action="" method=post enctype=multipart/form-data><p>
    <input type=file name=file><input type=submit value=Upload>
    </form>
    '''

@app.route("/download", methods=['GET'])
def download_file():
    return excel.make_response_from_array([[1,2], [3, 4]], "csv")

@app.route("/export", methods=['GET'])
def export_records():
    return excel.make_response_from_array([[1,2], [3, 4]], "csv", file_name="export_data")

# insert database related code here

if __name__ == "__main__":
    app.run()
相關文章
相關標籤/搜索