Hiển thị các bài đăng có nhãn export csv. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn export csv. Hiển thị tất cả bài đăng

Thứ Năm, 30 tháng 8, 2012

Export csv with web2py

In controller, forxample default.py:

def export_csv():
  
    animals = db().select(db.purchase_type.ALL)
    fname='hello.csv'
  
    return dict(animals1=animals, fname=fname)


In view: view/default folder:
make flie export_csv.csv:

{{
import cStringIO
stream=cStringIO.StringIO()
animals1.export_to_csv_file(stream)
response.headers['Content-Type']='application/vnd.ms-excel'
response.headers['Content-disposition']='attachment;filename='+fname
response.write(stream.getvalue(), escape=False)
}} 


Save to system file?, simple:
f = open('hello.csv', 'w')
f.write(stream.getvalue())
f.close()

Enjoy!