2014-03-26 148 views
1

我正在嘗試在Google App Engine中使用xlsxwriter,但我一直在獲取錯誤:'響應'對象沒有'tell'屬性。我看過以前的問題Using xlsxwriter in Google App Engine for Python & XlsxWriter object save as http response to create download in Django但我仍然收到此錯誤。我試圖修改他們在文檔中給出的示例https://github.com/jmcnamara/XlsxWriter/blob/master/examples/http_server_py2.pyGoogle App Engine中的Xlsxwriter錯誤

我目前在GAE中使用xlwt時沒有任何問題。

output = StringIO.StringIO() 

    # Even though the final file will be in memory the module uses temp 
    # files during assembly for efficiency. To avoid this on servers that 
    # don't allow temp files, for example the Google APP Engine, set the 
    # 'in_memory' constructor option to True: 
    workbook = xlsxwriter.Workbook(self.response.out,{'in_memory': True}) 
    worksheet = workbook.add_worksheet() 

    # Write some test data. 
    worksheet.write(0, 0, 'Hello, world!') 

    # Close the workbook before streaming the data. 
    workbook.close() 

    # Rewind the buffer. 
    output.seek(0) 

    # Construct a server response. 

    self.response.headers['Content-disposition'] = 'attachment; filename=test.xls' 
    self.response.headers['Content-Transfer-Encoding'] = 'Binary' 
    self.response.headers['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 

回答

1

你有幾個問題。

您正在創建一個StringIO,但您沒有使用它。你xlsxwriter.Workbook(應該使用outputself.response.out

其次你爲什麼要這麼做output.seek(0)

與工作簿關閉後,你應該從output獲取字符串,並編寫出self.response

最後一點,如果你是出現錯誤時,在問題中添加堆棧跟蹤到問題時總是很有用。通過這種方式,我們可以看到代碼中的哪個語句實際上正在引發異常。

+0

謝謝我通過插入self.response.out.write(output.getvalue())得到它的工作。 – nallad1985