tags:

views:

58

answers:

3

Hello,

am using xlwt module to create xls file with multiple sheets and i want to display Content disposition with the created file.

 wbk = xlwt.Workbook(encoding='utf-8')
 sheet = workbook.add_sheet('sheet1')

and then

 response.headers['Content-Type'] = \
    gluon.contenttype.contenttype('.xls')
response.headers['Content-disposition'] = 'attachment; filename=projects.xls'\

how can i make the content of wbk inside projects.xls?

Thanks in Advance

+1  A: 

Looking at the docs, it seems that you could write out to a StringIO, and then print that out.

import StringIO
output = StringIO.StringIO()
wbk.save(output)

and then use output.getvalue().

I have not tested this at all.

Xiong Chiamiov
The theory seems right
Ben
I suppose I should mention that, if using CPython, I'd probably use cStringIO. Whether or not you need it depends on your situation, though.
Xiong Chiamiov
A: 

Use the response.body attribute to write the excel file:

wbk.save(response.body)
ars
A: 

[dis]claimer: I'm the maintainer of xlwt.

Workbook.save(destination) ...

if destination is an object with a write method (e.g. obtained by [c]StringIO.StringIO()):

xlwt writes to this but doesn't close it. What you use this for and how you dispose of the object are up to you.

else:

xlwt assumes that destination is a string which is interpreted as a file path; xlwt attempts to open a file, write to it, and close it.

John Machin