Skip to content

Instantly share code, notes, and snippets.

@phucat
Created November 12, 2015 14:07
Show Gist options
  • Save phucat/405cbed9610c88b88c45 to your computer and use it in GitHub Desktop.
Save phucat/405cbed9610c88b88c45 to your computer and use it in GitHub Desktop.
excel export
# download xlsx writer here :XlsxWriter-0.7.7.tar.gz (md5)
#https://pypi.python.org/pypi/XlsxWriter#downloads
#extract and put to "lib" folder under root app directory
from cStringIO import StringIO
import xlsxwriter
out = StringIO()
wb = xlsxwriter.Workbook(out, {'in_memory': True})
data = SampleModel.query().filter().fetch()
# x and y axis (row and column on excel file)
x = 1
y = 0
for item in data:
v = item.get('fieldname')
sheet.write(x, y, v)
wb.close()
out.seek(0)
# this will return excel in binary format
return out.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment