2011-07-12 39 views
0

我目前正在用xlwt創建一個電子表格,並試圖將它導出爲django中的HttpResponse供用戶下載。我的代碼如下所示:導出xls文件用戶用django和HttpResponse下載的問題

response = HttpResponse(mimetype = "application/vnd.ms-excel") 
response['Content-Disposition'] = 'attachment; filename = %s +".xls"' % u'Zinnia_Entries' 
work_book.save(response) 
return response 

這似乎是做了正確的方式,但我得到一個:

Traceback (most recent call last): 
    File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 1233, in communicate 
    req.respond() 
    File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 745, in respond 
    self.server.gateway(self).respond() 
    File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 1927, in respond 
    response = self.req.server.wsgi_app(self.env, self.start_response) 
    File "C:\dev\workspace-warranty\3rdparty\django\core\servers\basehttp.py", line 674, in __call__ 
    return self.application(environ, start_response) 
    File "C:\dev\workspace-warranty\3rdparty\django\core\handlers\wsgi.py", line 252, in __call__ 
    response = middleware_method(request, response) 
    File "C:\dev\workspace-warranty\imcom\imcom\seo_mod\middleware.py", line 33, in process_response 
    response.content = strip_spaces_between_tags(response.content.strip()) 
    File "C:\dev\workspace-warranty\3rdparty\django\utils\functional.py", line 259, in wrapper 
    return func(*args, **kwargs) 
    File "C:\dev\workspace-warranty\3rdparty\django\utils\html.py", line 89, in strip_spaces_between_tags 
    return re.sub(r'>\s+<', '><', force_unicode(value)) 
    File "C:\dev\workspace-warranty\3rdparty\django\utils\encoding.py", line 88, in force_unicode 
    raise DjangoUnicodeDecodeError(s, *e.args) 
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte. You passed in 

(我離開的休息,因爲我得到了很長行這個\ xd0 \ xcf \ x11 \ xe0 \ xa1 \ xb1 \ x1a \ xe1 \ x00類的東西)

你們有什麼想法可能是錯誤的嗎?是因爲我的一些寫入值如下所示:

work_sheet.write(r,#,information)其中信息未轉換爲unicode?

+0

那麼......不是我所有的代碼,而是問題所在的部分。我可以提供更多,但我不確定寫入語句或工作簿創建如何導致錯誤。 – RJones

回答

0
response['Content-Disposition'] = 'attachment; filename = %s +".xls"' % u'Zinnia_Entries' 

應該只是

response['Content-Disposition'] = 'attachment; filename = %s.xls' % u'Zinnia_Entries' 

沒有引號的.xls否則輸出將

u'attachment; filename = Zinnia_Entries +".xls"' 

所以嘗試改變這一點。

但也檢查出這個答案。它有一個非常有用的輸出xls文件的小功能。

django excel xlwt

+0

明天我會看看第一部分,關於鏈接,我同意它的幫助。它實際上是我在這裏做了我想做的事情的地方(因爲功能內容與我的代碼基本相同)。 – RJones

+0

+事情不是問題,而是你的一部分。 謝謝你的收穫。這樣的小事已經成爲我以前存在的禍根。 – RJones

+0

@RJones - 你也刪除了qoutes? - 也許然後給我們一個你想要做什麼類型的寫作的例子。你也可以嘗試製作一個儘可能簡單的xls(我在考慮只有一些文本的領域)。試試看看是否可以添加一些你正在編寫的非Unicode數據。看看你是否可以用這種方式誘惑出錯。 – niklasdstrom

0

解決了這個問題。顯然有人把一些時髦的中間件的東西,這是黑客分開,追加和添加等。等。到文件。當它不應該。

無論如何,它完全沒有文件輸出。

@Storm - 感謝您的幫助!