在Web2py中使用Tablib库

2024-05-23 23:32:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经尝试了一段时间,使tablib与web2py的工作没有运气。代码按预期传递了一个.xls文件,但该文件已损坏且为空。你知道吗

import tablib 
data = []

headers = ('first_name', 'last_name')
data = tablib.Dataset(*data, headers=headers)

data.append(('John', 'Adams'))
data.append(('George', 'Washington'))


response.headers['Content-Type']= 'application/vnd.ms-excel;charset=utf-8'
response.headers['Content-disposition']='attachment; filename=test.xls'
response.write(data.xls, escape=False)

有什么想法吗?? 谢谢!你知道吗


Tags: 文件代码nameimportdataresponsecontentxls
1条回答
网友
1楼 · 发布于 2024-05-23 23:32:29

根据http://en.wikipedia.org/wiki/Process_stateresponse.write被记录为服务

to write text into the output page body

(我的重点)。data.xls不是文本它是二进制的东西!要验证这确实是问题的原因,请尝试改用data.csv,并且应该有效,因为它是文本。你知道吗

我相信您需要使用response.stream来发送“二进制内容”作为您的响应(或作为附件)。你知道吗

相关问题 更多 >