Greetings,
I went through several old posts and pages looking for a way to export
to csv. I cobbled this together and thought it might be useful to the
community.
To use it, place the attached file as <yourapp>/views/generic.csv and do
the following:
response.view='generic.csv'
return dict(filename='preferred_file_name.csv',csvdata=record_set)
filename is optional, and will default to export.csv
csvdata is required, and you'll get a ticket/exception if you don't pass it
Comments and suggestions appreciated.
--
Andrew Thompson
http://aktzero.com/
{{
###
# response._vars contains the dictionary returned by the controller action
# for this to work the action must return something like
#
# dict(filename=...,csvdata=...)
#
# filename is the suggested name to save the file as
# csvdata is the recordset to output
###
try:
thefile = filename
except:
thefile = 'export.csv'
pass
try:
import cStringIO
stream=cStringIO.StringIO()
csvdata.export_to_csv_file(stream)
response.headers['Content-Type']='application/vnd.ms-excel'
response.headers['Content-disposition']='attachment; filename=' +thefile
response.write(stream.getvalue(), escape=False)
except:
raise HTTP(405,'no css')
}}