Using swedish special characters åäö

139 views
Skip to first unread message

Kenneth Lundström

unread,
Nov 28, 2010, 4:11:40 PM11/28/10
to web...@googlegroups.com
I have no problem using ��� when reading and writing these characters
from database, also no problem displaying them on screen.

But when sending them to a CSV file and opening it in Excel ��� are not
shown correctly.

Has anybody succeeded in this?


Kenneth

Mirek Zvolský

unread,
Nov 29, 2010, 9:41:29 AM11/29/10
to web2py-users
> Swedish special characters...
> But when sending them to a CSV file and opening it in Excel are not
> shown correctly.

Here my code (functions in controller). I'm not sure if this is
correct solution, but it works for me. I think response header
'Content-Type' with 'utf-8' is important.
My code was tested for czech language and czech localized Excel,
however I hope it will work for swedish too, because swedish is some
kind of czech language?

Note, that there is bug in Opera browser at this time. Opera download/
saves the file correctly to disk, however with Open button the oldest
from saved csv files go to open. Regardless, if you use same name, or
if you change it (as in following code, where I add current time to
the filename). So you have to delete older file earlier, or just Save
it and open it in Excel from the disk, not directly from browser.
Other browser behaves correctly.

def excel():
import StringIO
sIO = StringIO.StringIO()
rsTable = db().select(db.tablename.ALL)
rsTable.export_to_csv_file(sIO, null='', delimiter='|')
__responseHeaders('exportedtable' +
datetime.datetime.now().strftime('_%Y%m%d_%H%M%S') + '.csv')
cFileContent = sIO.getvalue()
sIO.close()
return cFileContent

def __responseHeaders(fName=None):
response.headers['Content-Type'] = 'text/csv; charset=utf-8'
# duplicita s gluon\main.py, ale Expires se tam dělá pomocí time
objektu
response.headers['Cache-Control'] = 'no-store, no-cache, must-
revalidate, post-check=0, pre-check=0'
#response.headers['Cache-Control'] = 'no-cache, private, no-store,
must-revalidate, max-stale=0, post-check=0, pre-check=0'
response.headers['Expires'] =
datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT') #
Tue, 02 Nov 2010 08:15:33 GMT
# nebo lze .Expires = -1
response.headers['Pragma'] = 'no-cache'
if fName:
response.headers['Content-disposition'] = 'attachment;
filename="' + fName + '"'

Kenneth Lundström

unread,
Nov 29, 2010, 12:32:12 PM11/29/10
to web...@googlegroups.com
Hello Mirek,

thank you for your example. In my case it didn´t help. If I look at the
file in Notepad åäö are shown correctly by when opening the file by
dubble clicking it opens Excel but åäö aren´t shown correctly, for some
reason Excel uses "wrong" charset, if I import the file to Excel with
wizard I can select correct charset adn everything is shown correctly.

But customer want´s to make as easy as possible to open the file. Maybe
I have to scan all texts and replace åäö with another special character
that opens correctly in Excel.

I wrote åäö in Excel and saved it as an CSV, when looking at the file in
notepad I can find out what character to replace with.


Kenneth

Reply all
Reply to author
Forward
0 new messages