characters with accent sor umlauts get written wrong

J

John Dalberg

I am using the code snippet below. If the datagrid displays words with
french accent 'e' like 'cafe' or 'Toshiba Protege', the file test.xls
displays these e's as garbled 3 characters.

Do I need to do some formatting or specify some code page, langauge..etc?

StreamWriter sr;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new
HtmlTextWriter(sw);
myDataGrid.RenderControl(htw); sr =
File.CreateText("test.xls");
sr.WriteLine(sw.ToString());


John Dalbeg
 
J

Joerg Jooss

Thus wrote John,
I am using the code snippet below. If the datagrid displays words with
french accent 'e' like 'cafe' or 'Toshiba Protege', the file test.xls
displays these e's as garbled 3 characters.

Do I need to do some formatting or specify some code page,
langauge..etc?

File.CreateText() uses UTF-8 as character encoding. It seems Excel (I'm using
2003 SP2) doesn't correctly decode such a file if it is simply opened by
double clicking. But if you open the file from within Excel, you can choose
the correct encoding and process the file successfully.
StreamWriter sr;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new
HtmlTextWriter(sw);
myDataGrid.RenderControl(htw); sr =
File.CreateText("test.xls");
sr.WriteLine(sw.ToString());

As a workaround, simply create your text file with a Windows specific encoding
(Windows-125x), usually exposed as Encoding.Default.

sr = new StreamWriter("test.xls", false, Encoding.Default);

If you can't use the default settings for file access applied by StreamWriter
internally, create a FileStream with your specific accesss flags set first,
and pass that to your StreamReader:

sr = new Streamwriter(fileStream, Encoding.Default);

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top